We are no longer performing the processing in the class ThreadFunc. However, we will still use it to create the instances of the FileWorker class, set the options, and start the processing. If we don’t, we could cause <OnStart> to take too long to complete and return control to the service control manager (SCM). Again, it is never a good idea to tie up the <OnStart> method. Listing 1 shows the modifications required to our <ThreadFunc> method to support the new multi-file processing capabilities.
Listing 41. Updated Tutorials.ThreadFunc.
Private Sub ThreadFunc() Try Dim tmpOptions(5) As FileWorkerOptions Dim iLoop As Short Dim tmpWorker As FileWorker For iLoop = 0 To 4 tmpOptions(iLoop) = New FileWorkerOptions tmpOptions(iLoop).Output = My.Resources.OutgoingPath Next tmpOptions(0).FileType = My.Resources.FileType1 tmpOptions(0).Input = My.Resources.IncomingPath tmpOptions(1).FileType = My.Resources.FileType2 tmpOptions(1).Input = My.Resources.IncomingPath2 tmpOptions(2).FileType = My.Resources.FileType3 tmpOptions(2).Input = My.Resources.IncomingPath3 tmpOptions(3).FileType = My.Resources.FileType4 tmpOptions(3).Input = My.Resources.IncomingPath4 tmpOptions(4).FileType = My.Resources.FileType5 tmpOptions(4).Input = My.Resources.IncomingPath5 For iLoop = 0 To 4 m_WorkerThreads.Add(New FileWorker(m_ThreadAction, tmpOptions(iLoop))) tmpWorker = m_WorkerThreads(iLoop + 1) tmpWorker.Start() Next Catch fio As IOException WriteLogEvent(My.Resources.ThreadIOError + Now.ToString, THREAD_ABORT_ERROR, EventLogEntryType.Error, My.Resources.Source) Me.Stop() Catch tab As ThreadAbortException 'this must be listed first as Exception s the master catch 'Clean up thread here WriteLogEvent(My.Resources.ThreadAbortMessage + Now.ToString, THREAD_ABORT_ERROR, EventLogEntryType.Error, My.Resources.Source) Catch ex As Exception WriteLogEvent(My.Resources.ThreadErrorMessage + Now.ToString, THREAD_ERROR, EventLogEntryType.Error, My.Resources.Source) Me.Stop() End Try End IfEnd Sub |
The previous code listing creates five instances of the FileWorkerOptions class—one instance for each instance of FileWorker that we plan to create. Then it instantiates and sets the option properties. Finally, it creates five instances of the FileWorker class, passing each instance a reference to the ThreadActionState shared variable and the instance-specific FileWorkerOptions settings, and then starts that instance’s thread function.
I never recommend that you hard-code options into your application. Even if you use resource files, you have a lack of control over your service. You need to create an application configuration file that will store the settings for your application. Not only will this solve the hard-coding issue, but it will also shorten your code because it allows you to perform almost all the actions in a single loop.
Updating the <OnStop> Method
The last thing we need to update is the <OnStop> method. Currently we are using the Join method of the global m_WorkerThread to validate that processing is complete. In this new case we have multiple threads running at the same time.
We must perform several steps to update this method:
1. | Set the thread state to Stopped.
|
2. | Ask the SCM for extra time so that we can perform the required cleanup.
|
3. | Attempt to join each thread to validate that it has shut down, requesting additional time from the SCM for each thread we join. This might cause the service to take as much as 45 seconds to shut down, because the time-out is 15 seconds and we have five threads. However, this additional time helps to ensure that we are shutting down properly and causes the SCM to believe we processed the shutdown request properly. If the SCM believes we have not responded to the shutdown request properly, it will display an error stating that the service did not respond to the request in a timely manner. At this point you may need to use Task Manager to end the service process, unless you were using all background threads, which would be cleaned up automatically once the service instance shut down.
|
Note
Because the worker threads are background threads, if a Join times out, we exit the <OnStop> method and the service ends, these threads will be cleaned up, unlike non-background threads, which would keep the service running. |
Listing 2 demonstrates how to properly join each thread and attempt to shut down the service gracefully.
Listing 2. Update <OnStop> method to shut down smoothly.
Protected Overrides Sub OnStop() Try WriteLogEvent(My.Resources.ServiceStopping, ONSTOP_INFO, _ EventLogEntryType.Information, My.Resources.Source) m_ThreadAction.StopThread = True For Each fw As FileWorker In m_WorkerThreads Me.RequestAdditionalTime(THIRTY_SECONDS) fw.WorkerThread.Join(TIME_OUT) Next Catch ex As Exception WriteLogEvent(My.Resources.ServiceStopping + ex.ToString(), _ ONSTOP_ERROR, EventLogEntryType.Error, _ My.Resources.Source) End TryEnd Sub |
Installation and Verification
Now you can compile and install the latest version of the service. Remember to create the five folder locations that we specified in the resource settings. Depending on the file locations you created and the file types you are monitoring for, you have to remember to create files for your service to monitor and process.
For each file that you add to your monitoring folders, you will see the file move to your output folder and an entry in the event log. Once you have verified the service, stop and remove it before moving to the next section.
Note
Remember not to set two instances to the same file type—this will produce unexpected results, such as FileIOExceptions, because one thread has already moved a file that another thread just became aware of and tried to process. |