programming4us
           
 
 
Programming

Programming Windows Services with Microsoft Visual Basic 2008 : Services and Polling - Adding a Module File, Adding New Polling Code

- Windows 10 Product Activation Keys Free 2019
- How to active Windows 8 without product key
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/8/2013 9:11:22 PM

1. Polling the File System

Services are great for acting as a polling mechanism. In this example we will be monitoring for files that are being added to a specified directory. Whether these files come from another computer, a specific user, MSMQ, an SQL job, or an FTP site with the appropriate privileges, we can monitor for files that arrive and then perform some predefined task. In this first section we will monitor a specific directory for new text files. We will log an event for each new text file that we find.

2. Adding a Module File

The service is going to continue to grow, so to help to keep things cleaner and more readable, let’s add a module file to the project. In the Solution Explorer, click the Tutorials project icon and select Add Module. Name the file modService.vb and click Add.

As shown in Listing 1, cut and paste the THREAD_WAIT definition from the Tutorials.vb file into the modService.vb and change the definition from Private to Public.

Listing 1. Move the THREAD_WAIT definition to the modService file.
Module modService
  Public Const THREAD_WAIT As Integer = 5000

Adding Event Log InstanceIds

I want to define some custom InstanceIds that we can use specifically when we log an event to the event log. Currently we are just hard-coding them, but adding definitions for them would be cleaner. So add the code shown in Listing 2 to your modService file.

Listing 2. New program constants.
Public Const ONSTART_ERROR As Integer = 1000
Public Const ONSTOP_ERROR As Integer = 1001
Public Const THREAD_ERROR As Integer = 1002
Public Const THREAD_ABORT_ERROR As Integer = 1003
Public Const ONSTART_INFO As Integer = 2000
Public Const ONSTOP_INFO As Integer = 2001
Public Const THREAD_INFO As Integer = 2002
Public Const ONPAUSE_INFO As Integer = 2003
Public Const ONCONTINUE_INFO As Integer = 2004


3. Adding New Polling Code

The next step is to create the code that will look for these files and then add an event into the Application log. To do this we must modify the current <ThreadFunc> method. Before we modify <ThreadFunc>, we need to add another import statement to the top of tutorials.vb that will be used interact with the file system. Although the import is not required, it makes it easier to create class instances of the IO namespace without having to declare the entire namespace for each variable declared.

Add the code shown in Listing 3 to the top of the Tutorials.vb file.

Listing 3. Add a new Imports statement.
Imports System.IO

We can now directly access the file system classes used to check for any text files, without using the entire namespace. We will also need a directory to monitor. You can create this directory anywhere you want. I have created a directory called “incoming” and placed it in my c:\temp directory.

Introduction to Instrumenting a Resource File

An important factor in programming is localization—displaying information to a user based on the geographic locale or local language used. You use a resource file and the .NET built-in localization management methods to help make this happen.

We will implement a resource file to keep track of any strings or text that we have previously coded into the service as well as any new resource strings that we will add later on as we build on the service.

Creating the Resource File

In addition to allowing us to handle localization issues better, a resource file gives us the ability to avoid issues such as passing literal strings to method calls, which Microsoft discourages. To create the resource file, select the Tutorials project name, right-click, and select Properties. Click the Resources tab. If a resource file is not available, you will be prompted to create one. After you have created the resource file you will see a grid structure where you can enter string literals, names, and descriptions.

Not only will we add new strings, but also previously coded literals. Then I will demonstrate how to access these resources in your application.

First let’s add a string called IncomingPath and give it a value of c:\temp\incoming, or wherever you created your incoming folder. Leave the value for Description empty.

Adding Previous Literals to the Resource File

I have added the string literals shown in Table 1; we will use them to replace the currently hard-coded values in the service.

Table 1. List of Resources Created for This Service
NameValue YYY
IncomingPathc:\temp\incoming
SourceTutorials
ServiceStartingTutorials Starting
ServicePausingTutorials Pausing
ServiceStoppingTutorials Stopping
ServiceContinuingTutorials Continuing
ThreadMessageThread Function Information
ThreadAbortMessageThread Function Abort Error
ThreadErrorMessageThread Function Error
ThreadNameTutorials Worker Thread
ThreadIOErrorThread Function IO Error -
OutgoingPathc:\temp\outgoing

Microsoft Visual Basic makes it very easy to access resources through the Visual Basic My Object.

Using the My Object, you can access a resource by typing My.Resources.ResourceName. For instance, using the resource file you just created, My.Resources.IncomingPath returns the string c:\temp\incoming.

Other -----------------
- Microsoft Visual Studio 2010 : Using the Concurrency Visualizer (part 3) - The Cores View
- Microsoft Visual Studio 2010 : Using the Concurrency Visualizer (part 2) - CPU Utilization View, The Threads View
- Microsoft Visual Studio 2010 : Using the Concurrency Visualizer (part 1)
- Microsoft Visual Studio 2010 : Reports and Debugging - Using the Parallel Stacks Window
- Microsoft Visual Studio 2010 : Reports and Debugging - Using the Parallel Tasks Window
- Microsoft Visual Studio 2010 : Debugging with Visual Studio 2010 (part 2) - Debugging Threads
- Microsoft Visual Studio 2010 : Debugging with Visual Studio 2010 (part 1) - Live Debugging, Performing Post-Mortem Analysis
- .NET Components : Serialization and Class Hierarchies (part 2) - Manual Base-Class Serialization
- .NET Components : Serialization and Class Hierarchies (part 1) - Custom Serialization and Base Classes
- .NET Components : Custom Serialization (part 2) - Constraining Serialization
- .NET Components : Custom Serialization (part 1) - The ISerializable Interface, Implementing ISerializable
- .NET Components : Serialization and Streams - Serializing Multiple Objects
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 5) - Advanced HTTP Handler Programming
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 4) - Serving Images More Effectively
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 3) - The Picture Viewer Handler
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 2) - An HTTP Handler for Quick Data Reports
- Microsoft ASP.NET 3.5 : Writing HTTP Handlers (part 1) - The IHttpHandler Interface
- Microsoft ASP.NET 3.5 : HTTP Handlers and Modules - Quick Overview of the IIS Extensibility API
- Programming WCF Services : Queued Services - The HTTP Bridge
- Microsoft ASP.NET 4 : Ajax - Extender Controls (part 2) - A Modal Pop-up Dialog-Style Component
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

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

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS