cross thread events
-
i have posted almost the same question earlier, but this one is more straitforward: i want to handle events that raise by thread1 and process them with thread2, that's all! or i want to command thread2 to do sth from thread1
I had a look at your original post, and I think it will make it easier for you if you change your design a bit. If I uderstand correctly, you want to show your main form, and then show a splash screen right on top of it with a progress bar showing the advancement of some intialization work carried out by a background thread. Try this way: - Use the standard
Main()
for a Windows Form application, which will just run the app showing the main form (Form1
). - In theShown()
event handler forForm1
, show the splash screen (Form2
), create your thread, link its events to functions inForm2
and start the thread. - When you get an event from the thread, useInvoke()
to update the progress bar. As others told you, this is the best way to do that. - If you feel unsure aboutInvoke()
, set up a thread-safe (usinglock
) property inForm2
and update it from the events handlers, then have a timer check it and update your progress bar. This is not good design, but it will work. I hope I got your problem correctly, and hope this can help you. :)2+2=5 for very large amounts of 2 (always loved that one hehe!)
-
i have posted almost the same question earlier, but this one is more straitforward: i want to handle events that raise by thread1 and process them with thread2, that's all! or i want to command thread2 to do sth from thread1
-
i have posted almost the same question earlier, but this one is more straitforward: i want to handle events that raise by thread1 and process them with thread2, that's all! or i want to command thread2 to do sth from thread1
If thread2 is a UI thread, all you need to do is call Invoke/BeginInvoke on one of the controls created on that thread. Otherwise, the typical way is to share a queue between the two threads, and have the second thread watch the queue and process commands as they come in.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro