Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
-
I have a tcp server that cooperates with a windows forms aplication. Now I have the following problem: When I run it outside Visual Studio everything works just fine. But when I want to debug the application inside Visual Studio I get the message: "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on." What can be the reason for this? Any suggestions?
-
I have a tcp server that cooperates with a windows forms aplication. Now I have the following problem: When I run it outside Visual Studio everything works just fine. But when I want to debug the application inside Visual Studio I get the message: "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on." What can be the reason for this? Any suggestions?
Just put
CheckForIllegalCrossThreadCalls = false;
in the constructor of your main form in your Windows Application. It will solve the problem.
Regards, Anindya Chatterjee[^]
-
Just put
CheckForIllegalCrossThreadCalls = false;
in the constructor of your main form in your Windows Application. It will solve the problem.
Regards, Anindya Chatterjee[^]
This is bad advice; it won't solve any problems, only mask them for a little while. The docs[^] state that illegal cross thread calls will throw exceptions when run outside of the debugger no matter what you do. The correct way to perform cross thread UI operations is to invoke the operation on the GUI thread. See here[^].
:badger:
-
I have a tcp server that cooperates with a windows forms aplication. Now I have the following problem: When I run it outside Visual Studio everything works just fine. But when I want to debug the application inside Visual Studio I get the message: "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on." What can be the reason for this? Any suggestions?
-
The 'solution' that was given first worked for me. Again my problem was only there when I was debugging in Visual Studio. Running the code in the normal way didn't pose any problems. So it seems to be only a VS issue!! I will read the article that you described and see if something in the code has to be changed. Although I doubt that since I make use of delegates and that should take care of the thread issues. Thanks for the feedback. Regards, Jan
-
This is bad advice; it won't solve any problems, only mask them for a little while. The docs[^] state that illegal cross thread calls will throw exceptions when run outside of the debugger no matter what you do. The correct way to perform cross thread UI operations is to invoke the operation on the GUI thread. See here[^].
:badger:
Right. :rose:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
Just put
CheckForIllegalCrossThreadCalls = false;
in the constructor of your main form in your Windows Application. It will solve the problem.
Regards, Anindya Chatterjee[^]
Extremely bad advice X|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
The 'solution' that was given first worked for me. Again my problem was only there when I was debugging in Visual Studio. Running the code in the normal way didn't pose any problems. So it seems to be only a VS issue!! I will read the article that you described and see if something in the code has to be changed. Although I doubt that since I make use of delegates and that should take care of the thread issues. Thanks for the feedback. Regards, Jan
jkpieters wrote:
since I make use of delegates and that should take care of the thread issues.
Only if properly written using some form of Invoke. They are not a "guaranteed" solution to the problem.
jkpieters wrote:
So it seems to be only a VS issue!!
No, it's not. If you enable the "CrossThreadCheck" property, you are not fixing the problem, nor all causes of it. You are mearly hiding the problem in certain situations.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Ya you are right the solution is not advisable. I agree with you, but in this case as it was mentioned as a VS only problems during debugging that's why I have told him to do this. This will atleast allow your code to debug in VS. That's what he needed only. But still it is not a good practice. Using of delegates is the good way to do it.
Regards, Anindya Chatterjee[^]
-
The 'solution' that was given first worked for me. Again my problem was only there when I was debugging in Visual Studio. Running the code in the normal way didn't pose any problems. So it seems to be only a VS issue!! I will read the article that you described and see if something in the code has to be changed. Although I doubt that since I make use of delegates and that should take care of the thread issues. Thanks for the feedback. Regards, Jan
It really doesn't matter if it's in or out of VS when it's running, if an app is throwing invalid cross-thread operation exceptions then it's not observing the "One GUI Thread" rule. The only reliable way to do so is to use the IvokeRequired/Invoke/BeginInvoke members of the Control class.
:badger: