Introduction
This sample code may be use as a basic skeleton for a multi-threaded
process or service.
Background (optional)
Article "A simple tutorial on Multithreaded Programming using C#" from
Zeeshan Amjad may be useful for beginners.
Using the code
To use the code:
- create a new Console Application (or a Windows Services),
- Replace
[STAThread]
attribute by [MTAThread]
attribute of the Main
method (for Console Application),
- add WorkerThread.cs and WorkerThreadManager.cs files to your projet,
- modify the
DoSomethingUseful
method by providing your task,
- create a new
WorkerThreadManager
,
- call
Start / Stop
methods.
That's it.
Example of basic main method:
[MTAThread]
static void Main(string[] args)
{
String sCmd; WorkerThreadManager oManager = new WorkerThreadManager();
for (;;)
{
Console.WriteLine("EXIT/START/STOP");
Console.Write("$ ");
sCmd = Console.ReadLine ();
if (sCmd.ToUpper() == "EXIT")
{
break;
}
if (sCmd.ToUpper() == "START")
{
}
else if (sCmd.ToUpper() == "STOP")
{
}
}
}
Points of Interest
This code is the basic skeleton of one of our high availability SMS
(Short Message System) platform. It actually handles a couple of million of SMS per month on a 24/7 basis.
History
- 24th March, 2003 - First revision.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.