Visual Studio 2005 windows service walkthru inquiry

CForsyth

Member
Joined
Jun 7, 2006
Messages
8
Programming Experience
Beginner
I'm new to Windows programming in Visual Studio.Net.

I have a ASP.Net 2.0 web application that requires a particular action to occur at a user-definable interval. (Namely an index job)

I assume that a Windows Service would accommodate this function nicely.

However, I am unable to find a good working example of how to build a Windows Service using Visual Studio 2005.

All I really need at this point is a simple “hello world” type walkthrough or example that actually works and is built with Visual Studio 2005.

I did find the one (url below), however when following the directions some parts seem to be left out or some things that were said to be automatic didn’t seem to happen. I assume that the article was written for the older version of Visual Studio.Net.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkwalkthroughcreatingwindowsserviceapplication.asp

Does anyone have a good example or walkthrough that I can follow to get an idea of how to build a Windows Service specifically using Visual Studio.Net 2005 Pro?
 
Well, I know this isn't exactly what you're asking for, but here example code for a Threaded service. I didn't use the IDE to create it, so I assume you could just pick "New Class" from the Project menu and then paste this code into the new file. The application code goes into the MainLoop function.

VB.NET:
Imports System
Imports System.Threading
Imports System.ServiceProcess
Public Class MyService
    Inherits ServiceBase
    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    ' The main thread
    <MTAThread()> _
    Public Shared Sub Main()
        Dim svcs() As ServiceBase
        svcs = New ServiceBase() {New MyService}
        ServiceBase.Run(svcs)
    End Sub
    Private Sub InitializeComponent()
        Me.ServiceName = "TestService"
    End Sub
    Private thr As Thread
    Private Stopped As Boolean
    Protected Overrides Sub OnStart(ByVal args() As String)
        thr = New Thread(AddressOf MainLoop)
        thr.Start()  
    End Sub
    Protected Overrides Sub OnStop()
        Stopped = True
    End Sub
    Private Sub MainLoop()
 While Not Stopped
            ' Do any real work in here
            Thread.Sleep(TimeSpan.FromSeconds(5))
        End While
    End Sub
 
End Class

And this is the code that wrote to install a service. Again just make a new .vb file and paste into it.
VB.NET:
Imports System
Imports System.Collections
Imports System.Configuration.Install
Imports System.ServiceProcess
Imports System.ComponentModel
<RunInstaller(true)> _ 
Public Class ServiceInstaller
    Inherits System.Configuration.Install.Installer
    Private si As System.ServiceProcess.ServiceInstaller
    Private pi As System.ServiceProcess.ServiceProcessInstaller
    public Sub New()
        MyBase.New()
        pi = New System.ServiceProcess.ServiceProcessInstaller()
        si = New System.ServiceProcess.ServiceInstaller()
        ' Service will run under system account
        pi.Account = System.ServiceProcess.ServiceAccount.LocalSystem
        ' Service will have Start Type of Manual
        si.StartType = System.ServiceProcess.ServiceStartMode.Manual
        ' TODO: Fixup the service name here
        si.ServiceName = "TestService"
        ' Add the installers
        Installers.Add(si)
        Installers.Add(pi)
    end Sub
End Class

And this was the line I used to compile it (at a command line):
VB.NET:
vbc.exe /t:exe /reference:System.dll,System.ServiceProcess.dll,System.Configuration.Install.dll /out:MySvc.exe ThreadedService.vb InstallService.vb

Hope this helps.
 
Back
Top