ENTERPRISE

Creating Interactive Windows Services : Services and Polling - Updating Tutorials.ThreadFunc

1/30/2015 1:15:38 AM

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.

Other  
  •  Windows System Programming : Exception Handling - Vectored Exception Handling
  •  Windows System Programming : Exception Handling - Example: A Console Control Handler
  •  Windows System Programming : Exception Handling - Console Control Handlers
  •  Microsoft Enterprise Library : Relieving Cryptography Complexity - Diving in with an Example (part 2) - Obtaining and Comparing Hash Values
  •  Microsoft Enterprise Library : Relieving Cryptography Complexity - Diving in with an Example (part 1) - Encrypting and Decrypting Data Using A Symmetric Provider
  •  Microsoft Enterprise Library : Relieving Cryptography Complexity - How Do I Use the Cryptography Block?
  •  Microsoft Enterprise Library : Relieving Cryptography Complexity - What Does the Cryptography Block Do?
  •  Review : Aztech FG7008GR(AC) 2400Mbps Gigabit Router
  •  Using Exchange Server 2010 Antispam Tools (part 6) - Sender Reputation
  •  Using Exchange Server 2010 Antispam Tools (part 5) - Sender Filtering, Sender ID
  •  
    Video
    Video tutorials
    - How To Install Windows 8

    - How To Install Windows Server 2012

    - How To Install Windows Server 2012 On VirtualBox

    - How To Disable Windows 8 Metro UI

    - How To Install Windows Store Apps From Windows 8 Classic Desktop

    - How To Disable Windows Update in Windows 8

    - How To Disable Windows 8 Metro UI

    - How To Add Widgets To Windows 8 Lock Screen

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010
    programming4us programming4us
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone