Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to update the GUI Controls from Background Worker process in c#?
Posted

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5.

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.

—SA
 
Share this answer
 
to update UI controls from background thread you need to use the Dispatcher of UI - Window and invoke it like
C#
_preShellWindow.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
             { 
MessageBox.Show("Hii");
}));


Also you can call Update Layout base on your requirement
C#
this.Dispatcher.Invoke(DispatcherPriority.Render, (Action)delegate()
              {
                  InvalidateArrange();
                  InvalidateVisual();
                  InvalidateMeasure();
                  UpdateLayout();
              });
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jun-15 2:58am    
How do you know that the inquirer uses WPF? How do you know that all those invalidates should be done, not anything else? If could be millions different things the inquirer tries to do, which you are totally unaware of. How do you know that parameterless Action should be used because no parameters are to be passed? What's the use of such code sample? It is totally arbitrary, makes no sense. You did not try to explain anything...

Besides, the turn "background thread" has nothing to do with the issue. It's only important that this is not a UI thread. Please read on the foreground and background threads, it's important to know; and you don't have a clue.

—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900