Question Progress bar for Windows Service

Nucleus

Active member
Joined
May 24, 2005
Messages
30
Programming Experience
Beginner
Hi,

I am using visual basic 2010 express for the past three days, and I am trying a few things out

I am using this to start a service

VB.NET:
    Private Sub StartService_Click(sender As System.Object, e As System.EventArgs) Handles StartService.Click

        Dim sc As New ServiceController("helpsvc")
        If sc.Status = ServiceControllerStatus.Running Then
            MessageBox.Show("Service is already running")
            Exit Sub
        End If
        sc.Start()
        sc.WaitForStatus(ServiceControllerStatus.Running)
        LabelStatus.Text = sc.Status.ToString
        greenpic.Visible = True
        redpic.Visible = False

    End Sub

And this to stop a service

VB.NET:
    Private Sub StopService_Click(sender As System.Object, e As System.EventArgs) Handles StopService.Click

        Dim sc As New ServiceController("helpsvc")
        If sc.Status = ServiceControllerStatus.Stopped Then
            MessageBox.Show("Service is already stopped")
            Exit Sub
        End If
        sc.Stop()
        sc.WaitForStatus(ServiceControllerStatus.Stopped)
        LabelStatus.Text = sc.Status.ToString
        redpic.Visible = True
        greenpic.Visible = False

    End Sub

I also added a progress bar, but after doing a lot of reading, I just can’t find the correct way of making it grow to 100% full when the service starts and empty again, and the same when the service stops.

Help?
 
Windows services don't have forms and controls, create a Windows Forms application or a WPF application if that is your need.
 
This is a windows form application. Here's the whole code.

VB.NET:
Imports System.ServiceProcess
Public Class ServiceStatus
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim sc As New ServiceController("helpsvc")
        If sc.Status = ServiceControllerStatus.Running Then
            LabelStatus.Text = sc.Status.ToString
        ElseIf sc.Status = ServiceControllerStatus.Stopped Then
            LabelStatus.Text = sc.Status.ToString
        End If

        If sc.Status = ServiceControllerStatus.Running Then
            greenpic.Visible = True
        ElseIf sc.Status = ServiceControllerStatus.Stopped Then
            redpic.Visible = True
        End If

    End Sub

    Private Sub StartService_Click(sender As System.Object, e As System.EventArgs) Handles StartService.Click

        Dim sc As New ServiceController("helpsvc")
        If sc.Status = ServiceControllerStatus.Running Then
            MessageBox.Show("Service is already running")
            Exit Sub
        End If
        sc.Start()
        sc.WaitForStatus(ServiceControllerStatus.Running)
        LabelStatus.Text = sc.Status.ToString
        greenpic.Visible = True
        redpic.Visible = False

    End Sub

    Private Sub StopService_Click(sender As System.Object, e As System.EventArgs) Handles StopService.Click

        Dim sc As New ServiceController("helpsvc")
        If sc.Status = ServiceControllerStatus.Stopped Then
            MessageBox.Show("Service is already stopped")
            Exit Sub
        End If
        sc.Stop()
        sc.WaitForStatus(ServiceControllerStatus.Stopped)
        LabelStatus.Text = sc.Status.ToString
        redpic.Visible = True
        greenpic.Visible = False

    End Sub
End Class
 
Whops, read the post too quickly.

WaitForStatus is a blocking call, nothing will happen in your windows app until that returns. What you should do is call that from a secondary thread (for example use a BackgroundWorker), and in form use one of the indefinite progressbar styles (Continuous or Marquee), when operation is complete you reset progressbar style (Blocks).
 
Back
Top