Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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
Updated 23-Oct-11 23:12pm
v2

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
 
Share this answer
 
Comments
RaisKazi 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.
Sergey Alexandrovich Kryukov 25-Oct-11 3:37am    
Thank you, Rais.
--SA
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.
 
Share this answer
 
Comments
CodingLover 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 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).
Sergey Alexandrovich Kryukov 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
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.
 
Share this answer
 
Comments
CodingLover 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)
Sergey Alexandrovich Kryukov 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
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900