我正在处理一个pipe理应用程序的问题。 我构build了一个接口,用于停止,启动和查询40多台服务器上的各种服务。
我正在查看service.controller,并成功地停止和启动各种button事件的服务,但现在我试图找出一种方法来返回服务状态的文本框,并每10秒查询服务状态或所以我觉得我正在打砖墙。
有没有人有任何提示或见解?
谢谢!!
您可以使用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 } }
也许你不想投票:
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
欢呼进化