Click here to Skip to main content
15,884,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i want to do a refresh for a control from another thread, i always getting this exception "Cross-thread operation not valid"
Posted

The standard way to do this is to Invoke onto the UI thread to update the control. Assuming you are using Windows Forms, you would do something like this:
C#
if (MyControl.InvokeRequired)
{
  MyControl.Invoke(new System.Action<string>(MyMethod), myUpdatignValue);
}
If you are using WPF, it would look like this
C#
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action<string>(MyMethod), myUpdatingValue);
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Feb-12 12:32pm    
Pete, the formatting was mangled because you forgot to escape <string> with HTML character entities. I tried to fix it, please check up the result.
--SA
Pete O'Hanlon 1-Feb-12 12:34pm    
Doh. Thanks mate. Much appreciated.
Sergey Alexandrovich Kryukov 1-Feb-12 12:42pm    
I have more detailed answer, which is useful for understanding, please see my answer.

Also, I discussed your InvokeRequired. This check is rarely needed, so I explained where to use it and where not.
--SA
Espen Harlinn 1-Feb-12 16:50pm    
5'ed!
In addition to the answer by Pete, some more detail:

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[^].

The call to Control.InvokeRequired may or may not be needed. Basically, it should be called on the control which is inserted to the same UI hierarchy as the control to be used for a call Control.Invoke or Control.BeginInvoke. That is, those controls do not have to be the same. If the calling thread is not the UI thread working with these controls, Control.InvokeRequired always returns true, false otherwise. As invocation methods are usually called from a non-UI thread (always true), the check is usually not required. The Control.InvokeRequired is needed in more rare situations where invocation is called from the method which is itself called sometimes from the correspondent UI thread (when invoke is not required) and sometimes form the some other thread (when invoke is required). In this way, the call to Control.InvokeRequired is needed pretty rarely.

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
 
Comments
Espen Harlinn 1-Feb-12 16:50pm    
5'ed!
Sergey Alexandrovich Kryukov 1-Feb-12 20:30pm    
Thank you, Espen.
--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