Oct 10 2007

Parameterized threading for your own good

Category: Tips and TricksJoeGeeky @ 06:14

The Benefits of multithreading application function are numerous and well documented so I will not bore you will all of that.  However setting up a parameterized threading routine is not all that obvious and you don't see much of this in the boggouspher. So with that in mind I will walk you through a simple example of how to set up a parameterized thread, which I believe is far more practical for most applications today. First lets look a standard thread invokation without any params.  This is pretty straight foward... 

1.  Write the method that you would like to call via an alternate thread.  

 

private void MyAsyncMethod()
{
    //Save the world here...
}

2.  Write the method that will invoke your new AsyncMethod.  In this method you will need to instantiate the thread and point it to the address of the method to be invoked and then you will need to start the thread.  If you read the details on the Thread class on MSDN you will see there are tons of things you can do, but to keep this simple we will simply start the thread.

public void InvokeMyAsyncMethod()
{
    Thread myThread = new Thread(MyAsyncMethod);
    myThread.Start();
}

Thats it...  Call your invoke method and you are all set.  If you look at the Thread Class Constructor you will quickly see there is no overload supporting an address assignment of a method with parameters.  In the real world, we often need to execute threads based on some set of parameters. To accomplish our goal we need to employ a class named "System.Threading.ParameterizedThreadStart".  Essentially this is a delegate that allows us to assign a Thread to specific signature supplying one parameter via the Threads Start method. Lets walk through an example.

1.  Write the parameterize method that you would like to call via an alternate thread. In this case we have to follow the signature x(object y).  

private void MyAsyncParamMethod(object paramObject)
{
    //Save the world here with parameters...
}

2.  Now lets write our parameter-aware invocation method.

public void InvokeMyAsyncParamMethod(object paramObject)
{
    ParameterizedThreadStart pts = new ParameterizedThreadStart(MyAsyncParamMethod);
    Thread myParamThread = new Thread(pts);
    myParamThread.Start(paramObject);
}

That it... At this point you can pass a single object in for the params and parse the object internally to process whatever you need.

This is were a little old-time developer religion comes in to play.  So often I see people sending dictionary objects, collections, or worse yet an arraylist.  While this can get the job done, I would recommend creating your own strongly typed parameter class.  This is easier to parse, maintain, extend, document, etc...  Also, you can place tighter controls on validating what can be submitted via these params.... and it not so sloppy.  

 

Tags: ,

Comments

1.
Avery Wallace Avery Wallace United States says:

While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find.  I do appreciate that you've added relevant and intelligent commentary here though.  Thank you!

Comments are closed