Multithreaded windows service
-
Hi experts, I wanted to create a multi threaded windows service, can anyone reply me with the code, i will appreciate it. The scenario is, i have multiple databases, and i have to connect to each database and do some work after each interval using windows service. I could not able to find a simple multi threaded windows service code sample anywhere :confused: Help needed, thanks, Vijay Vijay
-
Hi experts, I wanted to create a multi threaded windows service, can anyone reply me with the code, i will appreciate it. The scenario is, i have multiple databases, and i have to connect to each database and do some work after each interval using windows service. I could not able to find a simple multi threaded windows service code sample anywhere :confused: Help needed, thanks, Vijay Vijay
I would be surprised if anyone would be able to post code for this type of request. I have written a few multithreaded services and can give you some pointers. 1. I usually put all of the code used by the thread in its own DLL, that way you can basically insantiate an instance of you class for each thread and limit the race conditions which can occur between threads. 2. Create a single public method in your class which can be the starting point for your thread, populate class properties with initial values in the Service code then use the ThreadStart or ParameterizedThreadStart methods to start the thread. Once the Thread has started (you can use ManualResetEvents to monitor this) add the Thread instance to an array in the Service code so all of the running threads canbe managed from the service. Doing so will allow you to stop the threads in the event that the service must stop. 3. I've found that .NET remoting is especiall handy with M-threaded Windows Services for sending control / command requests to the threads. Here is an example of starting a thread:
try
{
m_ThreadStarted.Reset();
//ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessADTThread), DR);
Thread ADTThread = new Thread(new ParameterizedThreadStart(ProcessADTThread));
ADTThread.IsBackground = true;
m_MasterThreadList.Add(ADTThread);
ADTThread.Start(DR);
m_ThreadStarted.WaitOne(-1, false);
Lines.Append(DR["ClientName"].ToString() + " Started\r\n");
}
catch (Exception Err)
{
m_ServiceLog.WriteEntry("Start Service WaitEvent Exception: " + Err.Message, EventLogEntryType.Error);
}