Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 20 Jan 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

  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.

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)

Share

About the Author

Grasshopper.iics
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.
Group type: Organisation

92 members

Follow on   Twitter   Google+

Comments and Discussions

 
SuggestionThe most common approach PinmemberInTRUEdeR21-Jan-13 0:04 
GeneralRe: The most common approach PingroupGrasshopper.iics21-Jan-13 2:03 
Hello. Thanks for the comment. Yes task scheduler is an option, but was only introduced in .Net 4.0.
 
For framework 3.5 and prior it does not work. So I posted it!

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411019.1 | Last Updated 21 Jan 2013
Article Copyright 2013 by Grasshopper.iics
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid