Click here to Skip to main content
12,943,528 members (57,483 online)
Rate this:
Please Sign up or sign in to vote.
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 9-Dec-12 19:16pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


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);

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
Thread th = new Thread(newClassInstance.DoHeavyOperation());

th.Start will execute the method DoHeavyOperation of newClass

hope this will clear your dbt.?

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,643
CHill60 2,970
Maciej Los 2,348
Jochen Arndt 1,900
ppolymorphe 1,765

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100