Visual Basic Express is a great, free tool from Microsoft. You don’t get all that Visual Studio 2005 offers though. One of the things VB Express does not have is templates to create a Windows Service.
We can still make Services with VB 2005 Express. All it takes is a little manual work. I will walk you thru a example on how to make a service in Visual Basic Express.
Choose Console Application, and give it the name of NewService1.
It will open in the designer with a module. Rename the module to NewService1.
Now we have to get some references for our Service. In the solution Explorer, double click on My Project and choose the References tab.
We need to Add the following references:
System.Configuration.Install
System.ServiceProcess
Now back to the NewService1 Module. We will be replacing all the text in the module, thus turning it into a class. Replace all the text with the text below:
1 Imports System.ServiceProcess
2 Imports System.Configuration.Install
3
4 Public Class NewService1
5 Inherits System.ServiceProcess.ServiceBase
6 Friend WithEvents Timer1 As System.Timers.Timer
7
8 Public Sub New()
9 MyBase.New()
10 InitializeComponents()
11 ‘ TODO: Add any further initialization code
12 End Sub
13
14 Private Sub InitializeComponents()
15 Me.ServiceName = “NewService1”
16 Me.AutoLog = True
17 Me.CanStop = True
18 Me.Timer1 = New System.Timers.Timer()
19 Me.Timer1.Interval = 6000
20 Me.Timer1.Enabled = True
21 End Sub
22
23 ‘ This method starts the service.
24 <MTAThread()> Shared Sub Main()
25 ‘ To run more than one service you have to add them to the array
26 System.ServiceProcess.ServiceBase.Run(New System.ServiceProcess.ServiceBase() _
27 {New NewService1})
28 End Sub
29
30 ‘ Clean up any resources being used.
31 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
32 MyBase.Dispose(disposing)
33 ‘ TODO: Add cleanup code here (if required)
34 End Sub
35
36 Protected Overrides Sub OnStart(ByVal args() As String)
37 ‘ TODO: Add start code here (if required)
38 ‘ to start your service.
39 Me.Timer1.Enabled = True
40 End Sub
41
42 Protected Overrides Sub OnStop()
43 ‘ TODO: Add tear-down code here (if required)
44 ‘ to stop your service.
45 Me.Timer1.Enabled = False
46 End Sub
47
48 Private Sub InitializeComponent()
49 Me.Timer1 = New System.Timers.Timer
50 CType(Me.Timer1, _ System.ComponentModel.ISupportInitialize).BeginInit()
51 ‘
52 ‘Timer1
53 ‘
54 Me.Timer1.Enabled = True
55 CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()
56 End Sub
57
58 Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As _ System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
59 Dim MyLog As New EventLog() ‘ create a new event log
60 ‘ Check if the the Event Log Exists
61 If Not MyLog.SourceExists(“NewService1”) Then
62 MyLog.CreateEventSource(“NewService1”, “NewService1 Log”) ‘ Create Log
63 End If
64 MyLog.Source = “NewService1”
65 MyLog.WriteEntry(“NewService1 Log”, “It is running”, EventLogEntryType.Information)
66
67 ‘disable the timer so you dont fill up the log
68 Timer1.Enabled = False
69 End Sub
70
71 End Class
72
This will create the NewService class and insert a Timer into your designer.
If you Double-Click on the NewService1 in the SolutionExplorer, you will see the designer and the Timer1 object.
This code will run after 6 seconds has passed, and will insert a log entry for our service saying “ It is running”.
We need to set the Service Name in the Designer:
Double click on NewService1 to get the gray page. Then click somewhere in the grey to get the properties for this class.
Under ServiceName put NewService1.
Services Part 2:> Creating the needed Installers
Programmers – Use your talents to make some money online. Click Here to Read More
Thanks for the tip!
I am going to add your blog to my list!