CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2008
    Posts
    43

    Starting a Windows Service Programattically

    I have a program which sets the .config file of a windows service and then starts the service. The problem I'm having i can only get the service to start if the program terminates immediately after starting the service, however i want to wait for 30 seconds and then stop the service before terminating the program.

    the code i use to start the service is:

    scnService = New ServiceController

    With scnService
    .MachineName = "."
    .ServiceName = "SERVICENAME"
    .Start()
    End With

    End

    After doing some investigating i found that if i put any code after .Start() the Status of the Service is set to 2 (StartWithPending - i don't know what this means) however if i immediately terminate the program the Status of the Service is set to 4 (Running). How do i set the Status to 4 without terminating the program?

  2. #2
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Starting a Windows Service Programattically

    Is the servicecontroller managed or unmanaged code? If managed, it should dispose when the variable with the object reference goes out of scope (probably whats happening when you exit your program). If its unmanaged, try calling scnService.Dispose()

    If unmanaged, and your using vs2005, you can do something like this:

    Code:
    Using scnService As New ServiceController
    With scnService 
    .MachineName = "."
    .ServiceName = "SERVICENAME"
    .Start()
    End With
    End Using
    Good luck
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  3. #3
    Join Date
    Apr 2008
    Posts
    43

    Re: Starting a Windows Service Programattically

    I tried both examples but go the same problem with each. also i didn't write the service so i don't have much information on it either which i know isn't very helpful

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Starting a Windows Service Programattically

    I just got hit by something ....

    Arn't you trying to start the service in the same thread as your App, try starting the Service in a new thread.

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Apr 2008
    Posts
    43

    Re: Starting a Windows Service Programattically

    i tried threading but it didn't work and i got the same problem. I don't really know much about threading so it may be that i didn't write the code properly. here's what i did:

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

    Dim t As Threading.Thread
    t = New Threading.Thread(AddressOf Me.StartService)
    t.Start()

    End Sub

    Private Sub StartService()

    With scnService
    .MachineName = "."
    .ServiceName = "SERVICENAME"
    .Start()
    .Dispose()
    Application.DoEvents()
    End With

    End Sub

    i also tried writing the StartService() sub like this and it still didn't work:

    Private Sub StartService()
    Using scnService As New ServiceController
    With scnService
    .MachineName = "."
    .ServiceName = "SERVICENAME"
    .Start()
    Application.DoEvents()
    End With
    End Using
    End Sub
    Last edited by johntheface; December 15th, 2008 at 05:31 AM.

  6. #6
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Starting a Windows Service Programattically

    I just tried this:

    Code:
        Private scnService As ServiceProcess.ServiceController
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            StartService()
            MsgBox("Done")
        End Sub
    
    
        Private Sub StartService()
            scnService = New ServiceProcess.ServiceController("Apache2.2", ".")
            scnService.Start()
        End Sub
    I didn't get the 'start with pending' status message in my services, just 'started' as expected. Can you post some sample code that reproduces the problem?
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  7. #7
    Join Date
    Apr 2008
    Posts
    43

    Re: Starting a Windows Service Programattically

    thank you everyone for your help. i now know the problem occurs if i try to write data to the .config file of the windows service using a StreamWriter immediately before starting the service. I'm assuming that my code is making the config file unreadable for the service?

    Code:
    Using swrConfigFile As New StreamWriter("C:\My Service.config")
            With swrConfigFile
                    .WriteLine(strConfigData)
                    .Close()
            End With
    End Using
    
    Dim scnService = New ServiceController("SERVICENAME", ".")
    scnService.Start()
    I know that the data being written to the file is correct and i've tried writing to the file in a Using statement and i've tried calling swrConfigFile.Dispose() once i've finished but i still get the error. any ideas?

  8. #8
    Join Date
    Apr 2008
    Posts
    43

    Re: Starting a Windows Service Programattically

    Quote Originally Posted by johntheface View Post
    I know that the data being written to the file is correct
    Actually it wasn't and that's what was causing the error. sorry for wasting everyone's time!

  9. #9
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Starting a Windows Service Programattically

    It comes with the territory, don't worry about it.. at least you found the problem
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

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

Featured