Click here to Skip to main content
15,883,843 members
Articles / Programming Languages / C#
Tip/Trick

WPF Dispatcher.Invoke Style UI Element Update for WinForm Applications in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
20 Jan 2013CPOL1 min read 54.3K   4   3
Simple code wrap for updating UI elements in cross thread applications

Introduction

Developers familiar with WPF use Dispatcher.Invoke to create a delegate for updating UI elements. The style is:

C#
  this.Dispatcher.Invoke(
                           DispatcherPriority.Normal,
                           (System.Windows.Forms.MethodInvoker)delegate()
                           {
// Your code here 
                           }); 

However Dispatcher is not available in Windows Forms. So generally Windows Form developers rely mainly upon updating the UI elements in ProgressChanged event handler of background worker. However, a simple and similar way of UI update would be nice as you may want several UI updates per progress change.

The solution might be known to many or should I say most of the prolific CodeProject developers. However I still find this question being asked in numerous forums and therefore have decided to post it here. If you already know this, great, if you do not, use it.

Using the Code

As the solution is simple, I would simply put the code. I guess nothing much is there to explain.

C#
this.Invoke((MethodInvoker)delegate { 
            
            // your UI update code here. e.g. this.Close();Label1.Text="something";
            });

One important thing that you must remember while working with updating UI elements is that it is always preferred to update the elements from UI thread. ProgressChanged is still the best place to update your UI . However it is a good practice to use this model even within ProgressChanged to ensure trouble free element update.

Points of Interest

Doing things in simpler ways is also an efficient way. Developers working extensively on cross threading models like Port programming, Web cam and Image operations, remote methods might find this technique useful. I posted as usual for beginners to save them from "Thread Horror". Your discussions, suggestions and criticism are welcome as always.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Integrated Ideas
India India
gasshopper.iics is a group of like minded programmers and learners in codeproject. The basic objective is to keep in touch and be notified while a member contributes an article, to check out with technology and share what we know. We are the "students" of codeproject.

This group is managed by Rupam Das, an active author here. Other Notable members include Ranjan who extends his helping hands to invaluable number of authors in their articles and writes some great articles himself.

Rupam Das is mentor of Grasshopper Network,founder and CEO of Integrated Ideas Consultancy Services, a research consultancy firm in India. He has been part of projects in several technologies including Matlab, C#, Android, OpenCV, Drupal, Omnet++, legacy C, vb, gcc, NS-2, Arduino, Raspberry-PI. Off late he has made peace with the fact that he loves C# more than anything else but is still struck in legacy style of coding.
Rupam loves algorithm and prefers Image processing, Artificial Intelligence and Bio-medical Engineering over other technologies.

He is frustrated with his poor writing and "grammer" skills but happy that coding polishes these frustrations.
This is a Organisation

115 members

Comments and Discussions

 
PraiseThreading Pin
Reeshabh Choudhary26-Dec-15 1:44
Reeshabh Choudhary26-Dec-15 1:44 
SuggestionThe most common approach Pin
intrueder20-Jan-13 23:04
intrueder20-Jan-13 23:04 
GeneralRe: The most common approach Pin
Grasshopper.iics21-Jan-13 1:03
Grasshopper.iics21-Jan-13 1:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.