Multithreading in C# Windows Form

August 23, 2008

What I write here was part of the application that we illustrated in this blog during our User Oriented Design lab course in summer semester 07.

 

Having only one thread doing both UI and processing would make the UI becoming unresponsiveness when there is a lot to process. We implemented a C# Windows Form application. There are 2 threads: UI thread and processing thread. 

In the beginning of the development, we did not expect the complexity of writing a multithread application would be very high.

 

1. Starting a thread

Following is an example of the function for a new thread to start.

public static void startClusterImages(object o)

{  mainForm = (MainForm) o;

….

Before I am going to start a thread, I check whether the processing thread has started or not. 

if (!clusterThread.IsAlive)
{

clusterThread = new Thread(threadFunction);
clusterThread.Start(this);

}

 The parameter for the ParameterizedThreadStart is the function that we want the processing thread to start.  I think that you can only pass one parameter. If there are multiple parameters, I suggest that you group all of paramaters into a single object. 

static ParameterizedThreadStart threadFunction = new ParameterizedThreadStart(ImageProcessingControl.startClusterImages);

static Thread clusterThread   = new Thread(threadFunction);

2. Control accessing shared variables

When accessing a shared variable, the lock mechanism is needed. However, I am not quite trusting the lock mechanism. It is better to design in a way which avoids using a shared variable.

Anyway, the “lock” keyword can be used to lock an object. The SyncRoot property of the object is used for locking.

lock (uodImageArrayList.SyncRoot)
{

}

3. Invoke a function in UI thread from another thread.

The processing thread can not directly invoke a function in a MainForm class which is in the UI thread. Following code is the way to access a function in the UI thread from the processing thread.

First, Check whether the call is from the UI thread or not. If this.InvokeRequired returns false, that means the call is from the UI thread. In this case, there is no problem.

However, if the call is not from the UI thread,  this.InvokeRequired  returns true. It will go to “else” statement and call BeginInvoke which it will call this setSelected_UODImageGroupUserControl  function again eventually. Somehow, the call thread will be switched to the UI thread, so when this setSelected_UODImageGroupUserControl   function is called again from beginInvoke, it is called from the UI thread.  

public delegate void setSelected_UODImageGroupUserControl_delegate(UserControl userControl);

public void setSelected_UODImageGroupUserControl(UserControl userControl)
{

if (this.InvokeRequired == false)
{

//If this function is called from the same thread (UI Thread).
//then proceed

this.currentlySelectedUserControl = userControl;
this.copyToolStripMenuItem.Enabled = true;
this.cutToolStripMenuItem.Enabled = true;
this.deleteToolStripMenuItem.Enabled = true;
this.toolStripStatusLabel1.Text = ” “;

 

}
else
{

//If this function is called from different thread
//Then call the delegate first
//The delegate will somehow route this request to be in the UI thread
this.BeginInvoke(new setSelected_UODImageGroupUserControl_delegate(

setSelected_UODImageGroupUserControl), new object[] { userControl });

 

}

}

Entry Filed under: Microsoft and .NET, User Interface Design and Technology. .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories

Top Posts

Recent Posts

Pages

Archives

 

August 2008
M T W T F S S
« Jul   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

Blogroll

Books

Blog Stats

Top Clicks

Category Cloud

database Distributed application Education emergency response entrepreneurship firefighter (Feuerwehr) Flex/Flash German Language (Deutsch) Java Microsoft and .NET Risk Management (Disaster) Software Testing Uncategorized User Interface Design and Technology wcf Windows Workflow

Spam Blocked