Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I was just curious to know why we get Cross thread exception when we access an UI element in Winform application whereas we don't find the same behavior when we access an member field?. My doubt is that since both UI element like a textbox and a member variable like an integer are members of Form class and they both should be created by the same thread, why is that they are treated differently?. As per my understanding by default there will be only one thread in a winform application.

Any help would be appreciated.
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[^].

Important: Please don't comment using "Submit your solution", which is reserved for the cases when you provide some help in response to a question. Use "Add comment" or "Reply" to existing comment.

—SA
 
Share this answer
 
@Harsha

You never get a cross thread exception if you are accessing the Controls in the same UI thread i.e your main thread.

you get this exception only when you try to access the UI Control in a different thread other than Primary thread. The secondary thread can be a Delegate method you invoked from a different thread or using ThreadPool.

since you said you get Cross Thread Exception can you paste your code where it throws this exception? from your message Its clear that you are trying to access the control inside the main thread. But it would be nice to share the code for the better understanding of what is happening at your side.
 
Share this answer
 
harsha,

Having the thread object as member of the class doesnt mean that it runs under the same thread as that of UI Thread. This code clearly explains all that you are accessing the UI control from a secondary thread created by the code

Thread th = new Thread(DoHeavyOperation);
th.Start();

here you are passing a method is nothing but the delegate. The Thread class creates a delegate that matches your method and executes in the secondary thread. It will accept any method which matches the delegate signature and its not mandatory to be the member of this class.

you can also pass
i.e
Thread th = new Thread(newClassInstance.DoHeavyOperation());
th.Start();

th.Start will execute the method DoHeavyOperation of newClass

hope this will clear your dbt.?
 
Share this answer
 

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