Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Properly stopping a .net windows service

Posted on 2009-07-01
4
Medium Priority
?
281 Views
Last Modified: 2013-12-17
How can I stop a vb.net windows service from within the code and have the service show as stopped in the WMI?  For example, if I am looking for a config file on startup and don't find it, I just want to stop the service.  I tried the code below, and it does stop executing the code in the DoWork routine (because the event log doesn't get wriitten to anymore), but the service still shows as started in the WMI.
Public Class TestService
 
    Private _worker As New Worker()
 
    Protected Overrides Sub OnStart(ByVal args() As String)
        Dim wt As System.Threading.Thread
        Dim ts As System.Threading.ThreadStart
        ts = AddressOf _worker.DoWork
        wt = New System.Threading.Thread(ts)
        wt.Start()
    End Sub
 
    Protected Overrides Sub OnStop()
        _worker.StopWork()
    End Sub
 
 
    Public Class Worker
 
        Private m_thMain As System.Threading.Thread
        Private m_booMustStop As Boolean = False
        Private m_rndGen As New Random(Now.Millisecond)
 
        Public Sub StopWork()
            m_booMustStop = True
            If Not m_thMain Is Nothing Then
                If Not m_thMain.Join(100) Then
                    m_thMain.Abort()
                End If
            End If
        End Sub
 
        Public Sub DoWork()
            Dim TestCounter As Integer = 0    'we'll stop the the service after we loop through twice
            m_thMain = System.Threading.Thread.CurrentThread
            Dim i As Integer = m_rndGen.Next
            m_thMain.Name = "Thread" & i.ToString
            While Not m_booMustStop
                TestCounter += 1
                System.Diagnostics.EventLog.WriteEntry("EmptyService", "Start work: " & m_thMain.Name)
                System.Threading.Thread.Sleep(10000)
                System.Diagnostics.EventLog.WriteEntry("EmptyService", "Finish work: " & m_thMain.Name)
                If TestCounter > 1 Then StopWork()
            End While
        End Sub
    End Class
 
End Class

Open in new window

0
Comment
Question by:grantdaddy
  • 3
4 Comments
 
LVL 14

Accepted Solution

by:
shahprabal earned 2000 total points
ID: 24757811
You need to use the System.ServiceProcess.ServiceController class :
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx
 
0
 

Author Comment

by:grantdaddy
ID: 24763405
Thanks shahprabal.  I tried the code below and it is kind of working.  The service still shows as Started in the WMI (even if you refresh the list or exit WMI and come back in) but if you try to stop the service through the WMI, you get a windows message box saying they service could not be stopped due to an internal error.  It is like the service really is stopped but WMI does not know it.

Is the code below what you would typically use?
Dim Services() As ServiceController = ServiceController.GetServices
For Each service As ServiceController In Services
   If service.ServiceName = "Test" Then
      service.Stop()
      Exit For
   End If
Next

Open in new window

0
 
LVL 14

Expert Comment

by:shahprabal
ID: 24763463
1) Check the value of service.CanStop
This should be true else the service is created such that it can't be stopped.
2) Add this line after service.Stop()
service.Refresh()
3) Then check the status property value:
service.Status()
0
 
LVL 14

Expert Comment

by:shahprabal
ID: 24763471
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question