C# - ThreadPool and Windows Forms

23. August 2011 22:31

 

This is a more complex example of using the c# thread pool in a windows forms application. The goal here is to background a task but still keep some / part of the interface active and not freeze as a task is running.

 

To get this example to work you will need to create a project and create a button and a text box. I have used btnRun and txtCount in this example. The real purpose of this example is to demonstrate how to disable / enable the gui and to background the task as well as provide status updates to the interface from the background task and to handle an error should it occur.

 

I should probably point out that you cannot directly update the forms gui in c# from a background thread. The Forms part of the .net frame work is not thread safe. If you attempt to do so it will however create an error such as, Cross-thread operation not valid: Control 'txtCount' accessed from a thread other than the thread it was created on.

 

To get around this issue you can test the property InvokeRequired. If this property is true then you know that you should not update the interface directly. Knowing this it is possible to still create functions that can still perform the functions on either the main program thread or the background thread by calling invoke when the InvokeRequired properly is true. When Invoke is called the framework will send a message then call the function on the correct thread.

 

The following example shows this.

 

 

public partial class frmMain : Form
{
	public frmMain()
	{
		InitializeComponent();
	}

	private void btnRun_Click(object sender, EventArgs e)
	{
		SetGUI(false);
		ThreadPool.QueueUserWorkItem(new WaitCallback(this.Run), null);
	}

	void Run(object args)
	{
		try
		{
			for (int i = 0; i < 10; i++)
			{
				UpdateStatus(i.ToString());
				Thread.Sleep(1000);
			}
			UpdateStatus("Complete");
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.Message);
		}
		
		SetGUI(true);
	}

	void UpdateStatus(object args)
	{
		if (InvokeRequired)
		{
			Invoke(new WaitCallback(UpdateStatus), args);
			return;
		}

		txtCount.Text = (string)args;
	}

	void SetGUI(object args)
	{
		if (InvokeRequired)
		{
			Invoke(new WaitCallback(SetGUI), args);
			return;
		}

		btnRun.Enabled = (bool) args;
	}
}

 

 

Note. I would normally create another class for the background task. Then drive the status updates, error and completion from events. It makes the code more maintainable and easyier to both read and test the background processes in the application. If you are intrested I will post an example of this as well.

 

E-mail Kick it! DZone it! del.icio.us Permalink


Pingbacks and trackbacks (1)+