Thursday, 2 September 2010

Simple Multi-threading Example

This example shows how to do processing on a separate thread so the thread that is handling drawing and updating the form does not get tied up on long running processes.

The program creates two threads. One which accepts a parameter and one which does not.

One thing to note in the code is the following snippet:

This ensures that a Cross Thread exception is not thrown. Basically a control cannot be updated by a thread other than the thread it was created on. As all controls are created on the main thread by the form a method must be Invoked on the control or the form rather than the alternate thread.

Full code listing below:

1 comment:

  1. Very useful, I always forget how to do this.