Write text in other thread
-
Hi, in my program i started a thread to do some work and i want to change the text on a Form in another thread. I always get an exception. Could someone tell me what's the easiest way to do that? I read about delegates etc. Sounds NOT so easy :D Thank you in advance.
-
Hi, in my program i started a thread to do some work and i want to change the text on a Form in another thread. I always get an exception. Could someone tell me what's the easiest way to do that? I read about delegates etc. Sounds NOT so easy :D Thank you in advance.
Indeed, all operations on a Control (creation and modification) must be handled by a single thread, normally that is the "UI thread". If another thread is involved, you must use Invoke (or BeginInvoke) to launch the operation on the right thread. There is a simple example in my article http://www.codeproject.com/csharp/LPSokoban.asp[^] Look at the method pasteOneMove(): it is called by "thread" which is not the UI thread; so pasteOneMove invokes itself, effectively making it run again, but this time on the UI thread. :)
Luc Pattyn
-
Indeed, all operations on a Control (creation and modification) must be handled by a single thread, normally that is the "UI thread". If another thread is involved, you must use Invoke (or BeginInvoke) to launch the operation on the right thread. There is a simple example in my article http://www.codeproject.com/csharp/LPSokoban.asp[^] Look at the method pasteOneMove(): it is called by "thread" which is not the UI thread; so pasteOneMove invokes itself, effectively making it run again, but this time on the UI thread. :)
Luc Pattyn