Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Threading
Hi all,
 
I am working on someone else WinForm application. There are lost of control manipulations such as SetText, Enable, Visible, AppendText... Now I want to do some improvements and I want to do it in a background thread. So I have two options to maintain valid cross-thread operations.
 
1. Use of InvokeRequired property on controls.
2. Set the Control.CheckForIllegalCrossThreadCalls property to false.

If I take the first way, I have lots of work to do with the existing codes. But the most suitable way.
 
If I take the second option, it'll stop all the exception. But not the best way to do.
 
So in your experience what is the most suitable option I have? Comments really appreciate.
Posted 24-Oct-11 0:08am
Edited 24-Oct-11 0:12am
RaisKazi33.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Both are not valid options.
 
First one is big no-no. It makes things worse as it can conceal a real problem. An application can even run successfully many times and later fail, and the cause of the problem will be very hard to find, nearly impossible.
 
Second one is not an option at all. Let's see.
 
This is just a predicate which allows a code to select weather System.Windows.Forms.Control.Invoke or System.Windows.Forms.Control.BeginInvoke, so the real option is using invocation, not InvokeRequired. You cannot call anything related to UI form a non-UI thread. You should always use invocation.
Now, what about InvokeRequired? It has limited need. First of all, it does not matter on which control Invoke or BeginInvoke can be called; it can be any control actually included in currently running UI be some UI thread. This predicate will always return true if you call it from any other thread, and will always return false if you call it in the same UI thread. In most cases, you know that by design, without calling InvokeRequired. This call may be needed only if you have some method can sometimes be called from the same UI thread, sometime from some other thread(s).
 
See my past solution where I explain all that in detail: Control.Invoke() vs. Control.BeginInvoke()[^].
 
—SA
  Permalink  
Comments
RaisKazi at 25-Oct-11 3:28am
   
My 5+. This is the most convincing Answer for this Question. Both the options mentioned by OP are really not solutions but invitation to disaster.
SAKryukov at 25-Oct-11 3:37am
   
Thank you, Rais.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Option 2 only suppresses a warning. All the ugly things that can happen with threading keep happening. Therefore Option 2 actually is not an option.
 
So go for first. It's the only one you have.
  Permalink  
Comments
CodingLover at 24-Oct-11 5:24am
   
Yes mate. But I have to do lots of work on it. Unfortunately that project initiator not even think about that at that time.
BobJanova at 24-Oct-11 6:29am
   
Then tell your management how much work it will be and see if they still want the improvements. Lukeer is right, 1 is the only way ... if you do 2 you will get weird stuff happening (which is why they brought that exception in in .net 2 in the first place).
SAKryukov at 24-Oct-11 20:35pm
   
No, this answer is not quite correct. This is invalid choice. Right thing is not Control.InvokeRequired, but Control.Invoke or Control.BeginInvoke. Control.InvokeRequired sometimes is needed, sometimes not. Please see my solution and referenced solution.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Without knowing the details, just be careful about changing the UI using background threads, you may
 
a) confuse the user, if the user changes something and then changes something else and a button is enabled - it is difficult for the user to know which change affected the buttons enabled status
 
b)if the user changes something and then changes something else and then a button is enabled and they click on it, the second change may invalidate the button being valid anymore
 
It can start to get very messy very quickly.
  Permalink  
Comments
CodingLover at 24-Oct-11 5:22am
   
That is true. And I have a better understand about the process going on in that application too. :) However, my question was how can I do this cross-thread operation in a better way.
 
I have a best way, but involve lots of work (option 1)
 
I have a worst way, but involve minimum changes (option 2)
SAKryukov at 24-Oct-11 20:36pm
   
It does not quite address the problem itself. This is invalid choice. Right thing is not Control.InvokeRequired, but Control.Invoke or Control.BeginInvoke. Control.InvokeRequired sometimes is needed, sometimes not. Please see my solution and referenced solution.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Even i would suggest option 1 as 2 is used
"When a thread other than the creating thread of a control tries to access one of that control's methods or properties, it often leads to unpredictable results."
 
For option 2 you have to give time, but will actually help you in getting what you are asking for.
  Permalink  
Comments
SAKryukov at 24-Oct-11 20:37pm
   
No, second one is not a valid option, strictly speaking. This is invalid choice. Right thing is not Control.InvokeRequired, but Control.Invoke or Control.BeginInvoke. Control.InvokeRequired sometimes is needed, sometimes not. Please see my solution and referenced solution.
--SA

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 24 Oct 2011
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