Curious Cross Thread Error
-
Maybe it is not so curious, but it is to me at least. I have a system.timers.timer declared and set up and then the code executes the final line in the routine is ProcessDuplicateMessage. In the ProcessDuplicateMessage routine I get this error: System.InvalidOperationException was unhandled by user code Message="Cross-thread operation not valid: Control 'lblReviewValue' accessed from a thread other than the thread it was created on." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.set_WindowText(String value) at System.Windows.Forms.Control.set_Text(String value) at System.Windows.Forms.Label.set_Text(String value) at ....FImportMsgProcess.ProcessDuplicateMessage() in C:\....\FImportMsgProcess.vb:line 41 at ....FImportMsgProcess.FormLoadComplete(Object sender, ElapsedEventArgs e) in C:\....\FImportMsgProcess.vb:line 134 at System.Timers.Timer.MyTimerCallback(Object state) Now obviously this is something to do with the timer, which I assume is running in a different thread, but my question is: How do I return the process to the thread for the form before calling the next routine. If I remove the system.timers.timer and add a forms.timer I don't get the problem. Just curious really since I have cured the immediate problem by using a forms.timer instead and I would like to know for another occasion. :)
-
Maybe it is not so curious, but it is to me at least. I have a system.timers.timer declared and set up and then the code executes the final line in the routine is ProcessDuplicateMessage. In the ProcessDuplicateMessage routine I get this error: System.InvalidOperationException was unhandled by user code Message="Cross-thread operation not valid: Control 'lblReviewValue' accessed from a thread other than the thread it was created on." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.set_WindowText(String value) at System.Windows.Forms.Control.set_Text(String value) at System.Windows.Forms.Label.set_Text(String value) at ....FImportMsgProcess.ProcessDuplicateMessage() in C:\....\FImportMsgProcess.vb:line 41 at ....FImportMsgProcess.FormLoadComplete(Object sender, ElapsedEventArgs e) in C:\....\FImportMsgProcess.vb:line 134 at System.Timers.Timer.MyTimerCallback(Object state) Now obviously this is something to do with the timer, which I assume is running in a different thread, but my question is: How do I return the process to the thread for the form before calling the next routine. If I remove the system.timers.timer and add a forms.timer I don't get the problem. Just curious really since I have cured the immediate problem by using a forms.timer instead and I would like to know for another occasion. :)
You're correct about the System.Timers.Timer "firing" your code on a seperate thread. Docs on the Timer class are here[^] You also cannot access UI controls from any other thread other than the thread that created the control, i.e.: the UI (or startup) thread. A good article on the hows and whys of this can be found here[^].
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
You're correct about the System.Timers.Timer "firing" your code on a seperate thread. Docs on the Timer class are here[^] You also cannot access UI controls from any other thread other than the thread that created the control, i.e.: the UI (or startup) thread. A good article on the hows and whys of this can be found here[^].
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...:rose:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
:rose:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
When I find a good article, I run with it.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
When I find a good article, I run with it.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...I'll try and create some more then... :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
You're correct about the System.Timers.Timer "firing" your code on a seperate thread. Docs on the Timer class are here[^] You also cannot access UI controls from any other thread other than the thread that created the control, i.e.: the UI (or startup) thread. A good article on the hows and whys of this can be found here[^].
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Thanks Dave, and of course Luc for the article.
-
Thanks Dave, and of course Luc for the article.
You're welcome. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).