Illegal Cross thread Error
-
Hi All Initially i had two applications which access a dll for its method for sending and recieving data.So i got a error saying illegal thread cross operation .So in order to avoid it , i created a delegate and then add a if condition if (this.InvokeRequired) { MyDelegateMethod theDelegateMethod = new MyDelegateMethod (this.DisplayMessage); this.Invoke(theDelegateMethod, new object[] { message }); } else { ReceiveMessage(message); } by doing this i could eliminate the error.Now when i changed one application which was in windows to a webserivce , i am not getting this invoke method ..so what will i do .. Can any one help me in this regard to overcome this situation And second one which i heard to overcome above situation was using System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; In window appn i am getting CheckForIllegalCrossThreadCalls properties but in webservice i am not getting it.. pls help me if any one has idea in it
Regards DilipRam
-
Hi All Initially i had two applications which access a dll for its method for sending and recieving data.So i got a error saying illegal thread cross operation .So in order to avoid it , i created a delegate and then add a if condition if (this.InvokeRequired) { MyDelegateMethod theDelegateMethod = new MyDelegateMethod (this.DisplayMessage); this.Invoke(theDelegateMethod, new object[] { message }); } else { ReceiveMessage(message); } by doing this i could eliminate the error.Now when i changed one application which was in windows to a webserivce , i am not getting this invoke method ..so what will i do .. Can any one help me in this regard to overcome this situation And second one which i heard to overcome above situation was using System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; In window appn i am getting CheckForIllegalCrossThreadCalls properties but in webservice i am not getting it.. pls help me if any one has idea in it
Regards DilipRam
Hi, the cross-thread problem is inherent in how controls work; they are not thread- safe, so the right approach is for each control to have only one thread ever working on it, normally that's the main thread aka GUI thread. there was no checking before .NET 2.0 and an app would work, partially work, suddenly hang, whatever if you did it wrong. .NET 2.0 checks and throws InvalidOperationException (as you know), unless you set CheckForIllegalCrossThreadCalls false, in which case you return to the pre 2.0 situation. I cant recommend that. The normal solution is similar to what you have, with one difference: the delegate and the else part of the if normally call the same method. In your code it is DisplayMessage and ReceiveMessage !? BTW: I am not sure what "this" is refering to in your web service, does it has controls while not having a GUI ?? :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi, the cross-thread problem is inherent in how controls work; they are not thread- safe, so the right approach is for each control to have only one thread ever working on it, normally that's the main thread aka GUI thread. there was no checking before .NET 2.0 and an app would work, partially work, suddenly hang, whatever if you did it wrong. .NET 2.0 checks and throws InvalidOperationException (as you know), unless you set CheckForIllegalCrossThreadCalls false, in which case you return to the pre 2.0 situation. I cant recommend that. The normal solution is similar to what you have, with one difference: the delegate and the else part of the if normally call the same method. In your code it is DisplayMessage and ReceiveMessage !? BTW: I am not sure what "this" is refering to in your web service, does it has controls while not having a GUI ?? :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Thanks for the reply regarding the DisplayMessage and ReceiveMessage first one is event handler delegate for recieving messages and second one is local method for processing the receiving message..just for readability..then regarding this i was just pasting the code in my windows applications..So what is ur suggestion..What i understand from your reply is that 2003 will not throw the illegal operation error and so i dont want to check it..Am i right..If i am wrong then what is best solution.
Regards DilipRam
-
Thanks for the reply regarding the DisplayMessage and ReceiveMessage first one is event handler delegate for recieving messages and second one is local method for processing the receiving message..just for readability..then regarding this i was just pasting the code in my windows applications..So what is ur suggestion..What i understand from your reply is that 2003 will not throw the illegal operation error and so i dont want to check it..Am i right..If i am wrong then what is best solution.
Regards DilipRam
Here's how you do it in .Net 2.0: http://www.codeproject.com/csharp/winformthreading.asp[^]
-
Here's how you do it in .Net 2.0: http://www.codeproject.com/csharp/winformthreading.asp[^]
-
Thanks for the reply regarding the DisplayMessage and ReceiveMessage first one is event handler delegate for recieving messages and second one is local method for processing the receiving message..just for readability..then regarding this i was just pasting the code in my windows applications..So what is ur suggestion..What i understand from your reply is that 2003 will not throw the illegal operation error and so i dont want to check it..Am i right..If i am wrong then what is best solution.
Regards DilipRam
Hi, if you get cross-thread exceptions, something is wrong in your app. Going back to 1.x or setting CheckCrossThread=false are two ways to hide the symptoms, they dont cure the disease. The cross-thread exceptions 2.0 and later are given are extra symptoms to make you aware there is a disease which you should fix. the right way is with Control.InvokeRequired and Control.Invoke; yuou normally have two choices: - make the delegate and the else part point to the same method; - make the delegate point to the method it is in (this is kind of a recursive call, but the second time around InvokeRequired will be false hence the else part will execute). :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi, if you get cross-thread exceptions, something is wrong in your app. Going back to 1.x or setting CheckCrossThread=false are two ways to hide the symptoms, they dont cure the disease. The cross-thread exceptions 2.0 and later are given are extra symptoms to make you aware there is a disease which you should fix. the right way is with Control.InvokeRequired and Control.Invoke; yuou normally have two choices: - make the delegate and the else part point to the same method; - make the delegate point to the method it is in (this is kind of a recursive call, but the second time around InvokeRequired will be false hence the else part will execute). :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Hi thanks for the reply,but the problem i am facing is that i was initially working with windows application ,now i want same method to implemented in webservice .but in webservice i am not getting invoke properties..Hope u understand my problem.Since i am not getting the invoke properties, i am not able to check that condition.. Any way to play with it.
Regards DilipRam