PDA

Click to See Complete Forum and Search --> : Threading in Windows Service


MadCatVB
Mar 28th, 2006, 05:03 AM
Hi all,

I'm currently working on a windows service application that will process data files in a specific location. These files can be directly sent to the location via HTTP or they have to be optained from a mailbox. I have a thread running in the service which checks the mail box folder every 10 seconds or so for any new mail. If it finds any then it processes the mail and strips any attachments to the processing folder.

What i want, and indeed what i'm having difficulty with is to have another thread running simultaneously that uses a FileSystemWatcher and as soon as a file is added to the processing folder it then processes and does what is required with it.

How would i go about calling the second thread?

At present i have code that looks like this:


protected override void OnStart(string[] args)
{
//Set delay period
System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
waitTime = 1000 * Convert.ToInt32((configurationAppSettings.GetValue("LogProcessing.DelayTime", typeof(string))));

IsProcessing = true;
//Create single threaded worker thread to process emails
workerThread = new Thread( new ThreadStart(SpawnThread));
workerThread.Name = "Thread1";
workerThread.Start();

}

private void SpawnThread()
{
IsProcessing = true;
while (IsProcessing == true)
{
try
{ //sleep in case interrupt has been thrown.
Thread.Sleep(100);

Thread spawnedThread = new Thread( new ThreadStart(RunAttachmentExtraction));
spawnedThread.ApartmentState = ApartmentState.STA;
spawnedThread.Name = "MailThread";
spawnedThread.Start();
spawnedThread.Join();
Thread.Sleep(waitTime);
//// //stop email processing loop from running for too long
//// Stop = true;

}
catch(ThreadAbortException ex)
{
eLog.WriteEntry(ex.Message, EventLogEntryType.Information);
}
catch(ThreadInterruptedException ex)
{
eLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
catch (Exception ex)
{
DAL.Debugging.DebugException("LogProcessing.SpawnThread()",ex.Message,EventLogEntryType.Error);
}
}
}



This works fine along with the relevent code to process the e-mails. When i tried to create another thread to execute the code to monitor the processing folder it never seems to get reached. I had just created another thread in the onStart method.

Where have i gone wrong with this and how can i go about creating the second thread to execute seperate code.

Thanks in advance

Grant

MadCatVB
Mar 29th, 2006, 03:03 AM
Is it even possible to have two continuously looping functions to be called by seperate threads in one windows service?