Click here to Skip to main content
15,894,265 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hai
How to check service is running or not after service is stopped or started ?

in my website i have 2 buttons for stop and other for start service,if stop or start service i will redirect the same page,in page load i will check service is running or not,if runninge i will show "Started" word in label and one started image,if it stopped i will show "Paused" word in label and one paused image.

But actually sometimes show both image and my codes is
VB
 Private Sub sampleService()
        imgbtnPlay.Visible = False
        imgbtnPause.Visible = False

        Dim myController As ServiceController
        myController = New ServiceController("sample Service")
        Thread.Sleep(3000)
        If myController.Status = ServiceControllerStatus.Stopped Then
            imgbtnPlay.Visible = True
            imgbtnPause.Visible = False
            lblStatus.ForeColor = Color.Red
            lblStatus.Text = "Paused"
        ElseIf myController.Status = ServiceControllerStatus.Running Then
            imgbtnPause.Visible = True
            imgbtnPlay.Visible = False
            lblStatus.ForeColor = Color.Green
            lblStatus.Text = "Started"
        End If
End Sub

why both image(imgbtnPause and imgbtnPlay) appears same time ?this function in page load event and when start or stop button clicked page is redirect to same page.
Follows codes are 2 image button click event
VB
 Protected Sub imgbtnPause_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs) Handles imgbtnPause.Click



        Dim myController As ServiceController
        myController = New ServiceController("sample Service")

        If myController.Status = ServiceControllerStatus.Running Then

            While myController.Status = ServiceControllerStatus.Running
                myController.Stop()
                imgbtnPause.Visible = False
                imgbtnPlay.Visible = True
                lblStatus.ForeColor = Color.Red
                lblStatus.Text = "Paused"
                ' Thread.Sleep(3000)
                myController.Refresh()
            End While
        End If
          Response.Redirect("Dashboard.aspx")
    End Sub

And this is for Play button click event

    Protected Sub imgbtnPlay_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs) Handles imgbtnPlay.Click




        Dim myController As ServiceController
        myController = New ServiceController("sample Service")

        If myController.Status = ServiceControllerStatus.Stopped Then
            While myController.Status = ServiceControllerStatus.Stopped
                myController.Start()
                imgbtnPause.Visible = True
                imgbtnPlay.Visible = False
                lblStatus.ForeColor = Color.Green
                lblStatus.Text = "Started"
                '  Thread.Sleep(3000)
                myController.Refresh()
            End While
        End If
  Response.Redirect("Dashboard.aspx")
    End Sub



May be behave like this,if i start service,it redirect same page and service is checking,if code pass through IF part ,service is not start so it show pause image ,then it pass else-if part that time service is running so it executes else-if part also,in that service is running,it show play image.i think if and else-if satisfy that condition so it executes both part so only it show both image.Bcz if service is start or stop immediately not affect in service ,it take time.
Regards
Aravind
Posted
Updated 19-Jan-14 18:03pm
v5
Comments
JoCodes 19-Jan-14 23:07pm    
"But actually sometimes show both image"- can you tell which scenario is shows both button at the same time/?
Aravindba 19-Jan-14 23:55pm    
Hai,Actually i dont know,my need is show only one image,in image button click event i will stop or start service,then immediately i redirect to same page (Dashboard.aspx),in page load i will call sampleService() function,this function check the service stauts ,in this function only check service and show the related image.If stop it show "pause image" ,if start it show "play image"
JoCodes 20-Jan-14 0:24am    
One of the reason can be as per the code you shared only Status checking is done for Stopped and running while there are chances of status can be in Paused,stoppending,pausepending,startpending etc. Try to debug and see whether the service is in any of these status too.Handle those status too.

1 solution

One of the reason can be as per the code you shared only Status checking is done for Stopped and running while there are chances of status can be in Paused,stoppending,pausepending,startpending etc. Try to debug and see whether the service is in any of these status and handle those status too.
 
Share this answer
 
Comments
Aravindba 20-Jan-14 0:43am    
thank for ur reply,if i use break point,it work correctly,i mean if i use sleep for 5 to 8 secs i work perfectly,but i wont use sleep method bcz it will take long time to page load.
How to check pending status like stop pending,start pending in vb coding ?
JoCodes 20-Jan-14 0:50am    
Try the below link which can help you
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.status(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
Aravindba 20-Jan-14 1:02am    
thanx, i think this will work,but i am not redirect page after service is start or stop,just use codes in img button click event.
JoCodes 20-Jan-14 1:18am    
Welcome. You can decide exactly when to redirect. But it would be better if given in the status condition checking code itself so that it redirects upon the actual status of the service.
Aravindba 20-Jan-14 1:49am    
hi thanks,i have another problem in silverlight can u pls click link ,if u know silverlight
http://social.msdn.microsoft.com/Forums/en-US/11b3663c-7a4b-491a-bb6c-a8bb8da81b72/why-i-cant-get-folder-structure-and-load-in-treeview-?forum=silverlightnet

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900