【问题标题】:Service.Controller Status / PollingService.Controller 状态/轮询
【发布时间】:2008-11-14 17:26:56
【问题描述】:
我正在使用的管理应用程序有问题。我正在构建一个界面,用于停止、启动和查询跨 40 台左右的服务器的各种服务。
我正在查看 service.controller 并已成功通过按钮事件停止和启动各种服务,但现在我正试图找出一种方法将服务状态返回到文本框并查询每个服务状态10 秒左右,我觉得我撞到了一堵砖墙。
有没有人有任何提示或见解?
谢谢!!
【问题讨论】:
标签:
c#
windows
visual-studio
winapi
【解决方案1】:
您可以使用 Timer 对象触发定期服务检查。您可以对 Elapsed 事件运行服务查询。
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
}
}
【解决方案2】:
也许你不想投票:
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
干杯
进化了