Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Threading
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
good
Please Sign up or sign in to vote.

Solution 1

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

Solution 3

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.?
  Permalink  
Rate this: bad
good
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 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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 205
1 Sergey Alexandrovich Kryukov 165
2 jlopez788 109
3 Rob Philpott 100
4 Prakriti Goyal 97
0 OriginalGriff 6,837
1 Sergey Alexandrovich Kryukov 5,589
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2014
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