ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  C# News Ticker Multithreaded Application
Subject:   UI is updated on incorrect thread
Date:   2002-12-08 15:05:59
From:   richardb
Unfortunately, this article demonstrates how hard it is to write correct multithreaded code. You should only update Windows Forms controls on the thread that created them. The correct procedure is to check the control.InvokeRequired property and if it return true, to call control.Invoke or control.BeginInvoke to marshal the update onto the correct thread.


In addition, this application will never close as the two spawned threads never end. As they are non background threads they will keep the process alive (irrespective of the form having closed). The correct procedure would be to call thread.Interrupt on each thread before callijng Join on them. This will cause an exception to be thrown in the thread function thus breaking out of the while loop.