C# - Abstract Background Thread / Task

1. October 2011 08:00

 

In my previous post about creating a background thread I created a simple class to start / stop and run a thread from a class which is simple to manage from the rest of your code eg in the gui code from the button. It moves a lot of the code from the gui part of the code base into more managable chunks. This however can be broken down even further so that we can reuse the same code without having to repeat it.

 

If you have not already you will probably have to read the previous post to be able to following what is happening here. In breif we created a thread to start / stop and run a background process.

 

In this example I am going to take the previous code and make it reusable by creating an abstract class. To create this I created a shared lib (a dll) so that the code can also be shared between multiple projects. I called this Stev.Threading which holds all my shared Threading code. I tend to mirror the ms namespace but it is prefixed with Stev rather than system.

 

To get started on this it is really a code organisation rather than a coding exercise. We simply take the variables the start / stop and part of the run function and create a new abstract class like the following. Then we have so reusable code for controlling threads.

 

public abstract class ThreadSimple
{
	protected Thread Thd = null;

	public void Start()
	{
		if (Thd != null)
			throw (new Exception("Thread Already Running"));
		Thd = new Thread(new ParameterizedThreadStart(Run));
		Thd.Start();
	}

	public void Stop()
	{
		if (Thd != null)
		{
			Thd.Abort();
			Thd = null;
		}
		else
		{
			throw (new Exception("Thread Already Stopped"));
		}
	}

	protected abstract void Run(object obj);
}

 

We will also need to update our ThdExample. The changes here will be to remove the Thd variable and the Start / Stop functions since we now have thoose in the abstract class above which we will inherit. We want to keep our local variables thogh because they are specific to this thread process and have nothing todo with "thread management". ThdExample should now look something like this.

 

public class ThdExample : ThreadSimple
{
	private int Value = 0;

	public ThdExample()
	{

	}

	protected override void Run(object args)
	{

		try
		{
			while (true)
			{
				Value++;

				Console.WriteLine("Value: {0}", Value);

				Thread.Sleep(1000);
			}
		}
		catch (Exception ex)
		{

		}
		Thd = null;
	}
}

 

So now we have a class which is a background thread but the only code required to be written is the background process that we are running. Though we still have a little issue with the state of the Thd variable. Personally I don't like modifiying variables which are stored in the parent class as it normally requires somebody to jump between files to understand what they are for. I will fix that in the next post and improve things a little further by adding some bullet proof error handling.

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


Pingbacks and trackbacks (1)+