Results 1 to 5 of 5

Thread: [RESOLVED] Stopping a Service using VB.Net

  1. #1

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Resolved [RESOLVED] Stopping a Service using VB.Net

    Hi guys

    I know the title of this thread is a show stopper for a lot of us . But what I want to do is stop two services from running for a period of time then restart them. I know I can do this from the command prompt by using net stop servicename then restart with net start servicename. I need these two services stopped to do some work on the database. I do this manually right now and want to automate the process. I tried process.start with the command net stop but that did not work out. If I try the Process List I get the process running but not the services.

    I'm actually doing this from a Script Task in SSIS (the script task uses VB.Net 2008).

    Any help please

    I'll understand if people don't want to reply as this can be used for things that we all try to prevent, but I figured I'd take a shot.

    Gary
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  2. #2
    Addicted Member
    Join Date
    Nov 2007
    Posts
    159

    Re: Stopping a Service using VB.Net

    Here are the 2 subroutines I use. You call them with StopService("servicename") and StartService("servicename")

    Code:
            Imports System.ServiceProcess
    
        Public Sub StopService(ByVal myServiceName As String)
    
            Dim DataSource As String = txtServer.Text
            Dim sStatus As String
            Dim myController As ServiceController
    
            myController = New ServiceController
            myController.MachineName = DataSource
            myController.ServiceName = myServiceName
    
            TextBox1.Text += "Stopping service """ & myServiceName & """...." & vbNewLine
            
            If myController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
                TextBox1.Text += "Service """ & myServiceName & """ is already stopped" & vbNewLine
            Else
    
                Try
                    myController.Refresh()
                    sStatus = myController.Status.ToString
                    myController.Stop()
                    myController.WaitForStatus(ServiceControllerStatus.Stopped)
                    TextBox1.Text += "Service """ & myServiceName & """ stopped..." & vbNewLine
                Catch exp As Exception
                    TextBox1.Text += "Could not stop service """ & myServiceName & """" & vbNewLine
                End Try
            End If
        End Sub
    
    
        Public Sub StartService(ByVal myServiceName As String)
    
            Dim DataSource As String = txtServer.Text
            Dim sStatus As String
            Dim myController As ServiceController
    
            myController = New ServiceController
            myController.MachineName = DataSource
            myController.ServiceName = myServiceName
    
            TextBox1.Text += "Starting service """ & myServiceName & """...." & vbNewLine
    
            Try
                myController.Refresh()
                sStatus = myController.Status.ToString
                myController.Start()
                myController.WaitForStatus(ServiceControllerStatus.Running)
                TextBox1.Text += "Service """ & myServiceName & """ started..." & vbNewLine
            Catch exp As Exception
                TextBox1.Text += "Could not start service """ & myServiceName & """" & vbNewLine
            End Try
    
        End Sub
    Last edited by christopherpm; Apr 26th, 2012 at 07:44 AM. Reason: modified code
    If my post helped you, please rate it. Thanks.

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Stopping a Service using VB.Net

    http://msdn.microsoft.com/en-us/libr...ontroller.aspx

    vb.net Code:
    1. ' Use Imports System.ServiceProcess - manually add a referemce to System.ServiceProcess.dll
    2.  
    3. Private Sub StopService(ByVal servicename As String)
    4.     Dim myService As ServiceController =
    5.         (From svc In ServiceController.GetServices() Where svc.ServiceName = servicename).ToArray()(0)
    6.      If myService.Status = ServiceControllerStatus.Running AndAlso myService.CanStop Then
    7.         myService.Stop()
    8.         myService.WaitForStatus(ServiceControllerStatus.Stopped)
    9.         myService.Start()
    10.     End If
    11. End Sub

  4. #4

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Stopping a Service using VB.Net

    What does

    Code:
    Dim DataSource As String = txtServer.Text
    represent? Do I need the machine name here?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [RESOLVED] Stopping a Service using VB.Net

    Thanks guys working for me now in SSIS.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width