Programming Windows Services with Microsoft Visual Basic 2008 : Implementing the Worker Class, Creating the FileWorkerOptions Class

10/15/2013 7:25:41 PM

1. Implementing the Worker Class

Before continuing, we need to make adjustments external to this class.

Adding a FileWorker Collection

We need to add a collection to the service that can hold the number of worker class instances we plan to create. As shown in Listing 1, we will create five worker threads. This number is simply for example purposes and could easily change on a dynamic basis, which I will demonstrate shortly.

Listing 1. Declaration for the worker collection in the Tutorials service class.
Private m_WorkerThreads As New Collection

Adding New File Type and Input Locations

With this implementation, we need to be able to supply five specific file locations or a combination of file locations and file types. These should be implemented as resource strings For a total of five input locations, I have to add four more. Table 1 lists the locations.

Table 1. New File Type Location

For the service to work properly, I need to add five unique file types. In this implementation we want to create the additional four folders listed previously. You can create them anywhere you wish, but make sure that the service resources are updated to reflect their location.

2. Creating the FileWorkerOptions Class

Each instance of the FileWorker class needs to know three things:

  • The file input location

  • The file output location

  • The file type to process

Therefore we will add another class called FileWorkerOptions. Instead of creating another class file, we simply add the code shown in Listing 2 to the FileWorker class .vb file.

Listing 2. Adding a new property class to FileWorker.vb.
Public Class FileWorkerOptions
Private m_Output As String
Private m_Input As String
Private m_FileType As String
Public Property Output() As String
Return m_Output
End Get
Set(ByVal value As String)
m_Output = value
End Set
End Property
Public Property Input() As String
Return m_Input
End Get
Set(ByVal value As String)
m_Input = value
End Set
End Property
Public Property FileType() As String
Return m_FileType
End Get
Set(ByVal value As String)
m_FileType = value
End Set
End Property
End Class

As shown in Listing 3, we now create an instance of FileWorkerOptions in the FileWorker class.

Listing 3. Create an instance of FileWorkerOptions.
Private m_FileWorkerOptions As New FileWorkerOptions

Updating the FileWorker Constructor

We need to modify the constructor to reflect the required parameters that need to be passed into the class instance when it is created. Because these are required parameters, we must remove the parameterless constructor. First, as shown in Listing 4, we need to pass in a reference of the ThreadActionState from the Tutorials service class as well as the FileWorkerOptions settings for each specific FileWorker class instance.

Listing 4. Update the FileWorker constructor.
Public Sub New(ByRef threadaction As ThreadActionState, ByVal _
fileworkeroptions As FileWorkerOptions)
m_ThreadAction = threadaction
m_FileWorkerOptions.Input = fileworkeroptions.Input
m_FileWorkerOptions.Output = fileworkeroptions.Output
m_FileWorkerOptions.FileType = fileworkeroptions.FileType
End Sub

We use a reference to ThreadActionState because it is a shared instance from the Tutorials service class. When the values of the ThreadActionState are updated, we want them reflected in each FileWorker class instance so that the threads will properly react to state changes.

Because each FileWorker has its own options, we need to have these options passed into the FileWorker class, then stored locally. We may want to expose these properties so that we can update them on the fly. For example, pause the service, update the options, and then continue.

Updating Our <FileWorker.ThreadFunc>

To reflect the changes to our processing based on a per FileWorker instance we have to update the <ThreadFunc> method to allow for the options we are passing into the class instance.

Listing 5. Updated FileWorker thread function.
For Each TextFile As String In My.Computer.FileSystem.GetFiles( _
m_FileWorkerOptions.Input, _
FileIO.SearchOption.SearchTopLevelOnly, _
If m_ThreadAction.Pause Or m_ThreadAction.StopThread Then
Exit For
End If
Dim FileOutput As String = My.Resources.OutgoingPath + "\" + _
My.Computer.FileSystem.MoveFile(TextFile, FileOutput)
WriteLogEvent(My.Resources.ThreadMessage + TextFile, THREAD_INFO, _
EventLogEntryType.Information, My.Resources.Source)
Catch ex As Exception
WriteLogEvent(My.Resources.ThreadErrorMessage + Now.ToString, THREAD_ERROR, _
EventLogEntryType.Error, My.Resources.Source)
End Try

Notice that now we are using the passed-in options for Output, FileType, and Input locations. This allows each instance of the class to monitor different locations and different file types. In this example we are using five different locations, one file type, and one file output path. We could easily use one location, five file types, and five output paths.

Before we continue to update the code we need to create the five file type entries in our resource file to reflect those we will monitor in our incoming paths. I have created the following entries listed in Table 2 in my Tutorials Resource file.

Table 2. New File Type Resource Entries

  •  Programming Windows Services with Microsoft Visual Basic 2008 : Extending the Threading Model
  •  Programming Windows Services with Microsoft Visual Basic 2008 : Writing a New Thread Method, Monitoring with Multiple Threads
  •  The HP Virtual Server Environment : Example nPartition Management Scenario (part 4) - Rebooting and Booting nPartitions
  •  The HP Virtual Server Environment : Example nPartition Management Scenario (part 3) - Creating a new nPartition
  •  The HP Virtual Server Environment : Example nPartition Management Scenario (part 2) - Viewing the Complex after Installing Hardware, Extending the Existing nPartition
  •  The HP Virtual Server Environment : Example nPartition Management Scenario (part 1) - Viewing the Configuration of an nPartition Complex
  •  The HP Virtual Server Environment : nPartition Management Paradigms (part 2) - Remote Management via an nPartition Paradigm, Remote Management via the MP Paradigm
  •  The HP Virtual Server Environment : nPartition Management Paradigms (part 1) - Local nPartition Management Paradigm
  •  The HP Virtual Server Environment : nPartition Servers - Data Maintained by the Management Processor
  •  The HP Virtual Server Environment : HP nPartition Server Overview
    Top 10
    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
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    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