Results 1 to 5 of 5

Thread: Starting a Windows Service

  1. #1

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    Starting a Windows Service

    Project is made up of
    form1.vb
    service1.vb

    When the user clicks on button1 on form1 i would like the service1 to run? and when they click on button2 i would like it to stop, does anyone know how to do this? thank you.
    also can the service read variables in a moduale?
    -gm

  2. #2
    Junior Member joan_fl's Avatar
    Join Date
    Aug 2002
    Location
    Tampa, FL
    Posts
    28
    The service can read an App.Config just like a regular .exe can.

    I'm not sure if a form can start and stop a service.. I would think it could. I've always got around this by having the service launch a thread that does all the work. The service just starts and stops the thread. Doing this allows you to have a Form or a custom user interface to start and stop the thread that does all the work. It's also nice so when you debug you dont have to attach to the process of the service.

    Hope this helps,

    Joan

  3. #3

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    start, stop service.

    here is code that will stop and start a service for anyone that is interested.

    Imports System.ServiceProcess

    Public Class Form1

    Dim myController As ServiceController

    Private Sub mnuStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuStatus.Click
    Try
    Dim strMsgbox As String

    myController.Refresh()
    lblStatus.Text = "Status: " & "Windows service is currently " & LCase(myController.Status.ToString)
    Catch err As Exception
    Dim strMsgbox As String

    lblStatus.Text = "Status: Make sure " & PublicVariables.strServiceName & " is installed on local machine."
    strMsgbox = MsgBox("An error has occured, make sure " & PublicVariables.strServiceName & " is installed on local machine.", MsgBoxStyle.Information + vbOKOnly, "Windows Service")
    End Try
    End Sub
    Private Sub mnuStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuStart.Click
    Try
    Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
    lblStatus.Text = "Status: Starting Windows Service..."
    myController.Start()
    Me.Cursor = System.Windows.Forms.Cursors.Default
    lblStatus.Text = "Status: Windows service started successfuly"
    Catch err As Exception
    Dim strMsgbox As String

    Me.Cursor = System.Windows.Forms.Cursors.Default
    myController.Refresh()
    lblStatus.Text = "Status: Windows service is currently " & LCase(myController.Status.ToString)
    strMsgbox = MsgBox("Windows service is currently " & LCase(myController.Status.ToString), MsgBoxStyle.Critical + vbOKOnly, "Windows Service")
    End Try
    End Sub
    end class

  4. #4
    Junior Member joan_fl's Avatar
    Join Date
    Aug 2002
    Location
    Tampa, FL
    Posts
    28
    here is code that will stop and start a service for anyone that is interested.
    I knew there was a way! Thanks!

  5. #5

    Thread Starter
    Hyperactive Member gmatteson's Avatar
    Join Date
    Feb 2002
    Location
    Rhode Island, USA
    Posts
    293

    welcome

    your welcome

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