|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionTo readers: This article was posted last week under a different title. I deleted it later because the code does not really make multithreading easier, it requires you to derive a class from my base class and then override a virtual method. The current version is much simpler. I hope you find it useful, too. When programming with C/C++, you can create a new thread using the Win32 API With .NET, threading is supposed to be easier. However, the thread function you use in .NET cannot take any input parameter directly. You need to do some work to pass parameters to your worker thread, you can find out how to do it from MSDN. In this article, I will introduce a C# class, The ThreadHelper classThe purpose of this class is to hide some boring details of using the .NET thread classes and make it easier for you to create a new worker thread in your application. If you are the kind of developer who always wants to know everything in your application, then this class is probably of little interest to you. Here are the public methods of the // the constructor
public ThreadHelper();
// set a parameter value for the method you want to invoke
public void SetParameter(Object oValue);
// set the delegate (pointer) of the method you want to invoke
publicvoid SetMethod(Delegate pMethod);
// start a new thread and execute the method you specified
public bool StartThread();
// wait for the worker thread to finish
// warning: this method will block until the new thread is terminated
public void WaitForThread();
// get the error string
public String GetErrorMessage();
// get the output value
public Object GetOutput();
// reset all data so that the object can be reused
public void Reset();
Suppose you have a class class MyDataClass
{
public static DataSet GetData(String sConnection, String sSQL)
{
// the implementation
};
};
You may want to call this static member function to get data from several different databases simultaneously. Using the
Here is the code for the above example: // define a delegate (pointer) for your method
public delegate String myDelegate(String sConnection, String sSQL);
...
// create three helper objects
ThreadHelper objHelper1 = new ThreadHelper();
ThreadHelper objHelper2 = new ThreadHelper();
ThreadHelper objHelper2 = new ThreadHelper();
// set the delegate (pointer) of your method
objHelper1.SetMethod(new myDelegate(MyDataClass.GetData));
objHelper2.SetMethod(new myDelegate(MyDataClass.GetData));
objHelper3.SetMethod(new myDelegate(MyDataClass.GetData));
// set the connection string parameter
objHelper1.SetParameter(sConnection1);
objHelper2.SetParameter(sConnection2);
objHelper3.SetParameter(sConnection3);
// set the sql statement parameter
objHelper1.SetParameter(sStatement1);
objHelper2.SetParameter(sStatement2);
objHelper3.SetParameter(sStatement3);
// start the worker thread
objHelper1.StartThread();
objHelper2.StartThread();
objHelper3.StartThread();
// wait for the worker thread to finish
objHelper1.WaitForThread();
objHelper2.WaitForThread();
objHelper3.WaitForThread();
// process the output of the method called in the new thread
Object oOutput = ObjHelper1.GetOutput();
...
As you can see, it is easy to call a method from a new worker thread with the help of Note: If your method has multiple parameters, you need to call A test applicationI have included a C# console application ThreadHelperTest.exe with this article. The application uses ...
public delegate bool TestCall(String sName, int nTest);
...
MyTest objTest = new MyTest();
ThreadHelper objHelper1 = new ThreadHelper();
ThreadHelper objHelper2 = new ThreadHelper();
objHelper1.SetMethod(new TestCall(objTest.Test));
objHelper2.SetMethod(new TestCall(objTest.Test));
objHelper1.SetParameter("Bill");
objHelper2.SetParameter("George");
objHelper1.SetParameter(1);
objHelper2.SetParameter(2);
objHelper1.StartThread();
objHelper2.StartThread();
objHelper1.WaitForThread();
objHelper2.WaitForThread();
String sError1 = objHelper1.GetErrorMessage();
String sError2 = objHelper2.GetErrorMessage();
if(sError1=="")
{
System.Console.WriteLine("Output of test 1: " +
objHelper1.GetOutput().ToString());
}
else System.Console.WriteLine("Error in test 1: " + sError1);
if(sError2=="")
{
System.Console.WriteLine("Output of test 2: " +
objHelper2.GetOutput().ToString());
}
else System.Console.WriteLine("Error in test 2: " + sError2);
The following is the console output from this test application: Hello, Bill
This is test 1
The thread id is 2
Hello, George
This is test 2
The thread id is 3
Hello, Bill
This is test 1
The thread id is 2
Hello, George
This is test 2
The thread id is 3
Hello, Bill
This is test 1
The thread id is 2
Hello, George
This is test 2
The thread id is 3
Output of test 1: True
Output of test 2: True
Note: The Thank you for reading my articles. Recent updates
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||