Service / Poll Status Service.Controller

I have a problem with the administrative application I'm working on. I create an interface for stopping, starting, and requesting various services on 40 servers.

I look at service.controller and successfully stop and start various services with button events, but now I am trying to find a way to return the status of the service in the text box and request the status of the service every 10 seconds or so, and I feel like I am pushing a brick wall.

Does anyone have any tips or insights?

Thank!!

+3
source share
2 answers

You can initiate a periodic maintenance check using the Timer object. You can run your service requests in the Elapsed event.

    private void t_Elapsed(object sender, ElapsedEventArgs e)
    {
        // Check service statuses
    }

, ToString() . , GUI , .

    private delegate void TextUpdateHandler(string updatedText);

    private void UpdateServerStatuses(string statuses)
    {
        if (this.InvokeRequired)
        {
            TextUpdateHandler update = new TextUpdateHandler(this.UpdateServerStatuses);
            this.BeginInvoke(update, statuses);
        }
        else
        {
            // load textbox here
        }
    }
+4

, :

Private serviceController As ServiceController = Nothing 
Private serviceControllerStatusRunning = False

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Try
        serviceController = New ServiceController("NameOfTheTheServiceYouWant")
        If serviceController.Status = ServiceControllerStatus.Stopped Then
            ' put code for stopped status here
        Else
            ' put code for running status here
        End If
        BackgroundWorker1.RunWorkerAsync()
    Catch ex As Exception
        MessageBox.Show("error:" + ex.Message)
        serviceController = Nothing
        Me.Close()
        Exit Sub
    End Try
End Sub

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    If serviceControllerStatusRunning Then
        serviceController.WaitForStatus(ServiceControllerStatus.Stopped)
        serviceControllerStatusRunning = False
    Else
        serviceController.WaitForStatus(ServiceControllerStatus.Running)
        serviceControllerStatusRunning = True
    End If
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
     if serviceControllerStatusRunning then
       ' put code for running status here
     else
       ' put code for stopped status here
     end if
     BackgroundWorker1.RunWorkerAsync() ' start worker thread again
End Sub

 Evolved

+2

Source: https://habr.com/ru/post/1699571/


All Articles