【问题标题】:VB.net - code execution order?VB.net - 代码执行顺序?
【发布时间】:2014-05-26 17:32:58
【问题描述】:

我有一个简单的 GUI,我只是存储系统信息并显示结果。

两个问题区域是detectADUCdetectExchange Sub。只有当我在 form_load 事件中将一个标签放在订单上方时,才会更新相应的标签。

我在想也许我需要发布一些东西?我在某处持有价值吗?请看下面。

检测交换

Private Sub detectExchange()

    Dim exchange As New ServiceController("Microsoft Exchange Service Host")

    Try
        If exchange.Status.Equals(ServiceControllerStatus.Running) Or exchange.Status.Equals(ServiceControllerStatus.StartPending) Then
            Label9.Text = "Detected - Running"
        End If
    Catch ex As InvalidOperationException
        Label9.ForeColor = Color.Red
        Label9.Text = "NOT INSTALLED"

    End Try

    If exchange.Status.Equals(ServiceControllerStatus.Stopped) Or exchange.Status.Equals(ServiceControllerStatus.StopPending) Then
        Label9.Text = "Detected - NOT RUNNING"
    End If

End Sub

检测ADUC

Private Sub detectADUC()
    Dim aduc As New ServiceController("Active Directory Domain Services")
    Try
        If aduc.Status.Equals(ServiceControllerStatus.Running) Or aduc.Status.Equals(ServiceControllerStatus.StartPending) Then
            Label10.Text = "Detected - Running"
        End If
    Catch ex As InvalidOperationException
        Label10.ForeColor = Color.Red
        Label10.Text = "NOT INSTALLED"

    End Try

    If aduc.Status.Equals(ServiceControllerStatus.Stopped) Or aduc.Status.Equals(ServiceControllerStatus.StopPending) Then
        Label10.Text = "Detected - NOT RUNNING"
    End If

End Sub

呼叫潜艇

Private Sub systemchecks_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    RichTextBox1.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & (": installation launched")
    Label12.Text = getExternalIP()
    getHostname()
    hardwareID()
    detectOS()
    detectADUC()
    detectExchange()
    hardwareID()
End Sub

如您所见,唯一不同的是变量名、服务名和它更新的标签。 - 但是只有一个标签会更新,这取决于我在 form_load 中首先调用的标签。

我尝试使用Me.Refresh,- 我尝试添加一个新类,然后创建一个公共共享子类,然后在 form_load 中引用它。

我什至将交换异常捕获重命名为ex2

有人可以解释这个问题,以便我理解并避免将来出现问题吗?

【问题讨论】:

  • 不是两个标签都会有文本Detected - NOT RUNNING,即使它们正在运行?
  • 我不明白你的意思,抱歉。标签确实可以正常工作。
  • 查看最后一个 If 语句。最后一个任务是Label10.Text = "Detected - NOT RUNNING",我认为它不应该存在。或者更好的是,完全删除该 IF 语句,因为您已经有一个 try..catch 事先
  • 不管 try catch 语句做了什么,你最后的 If-Then 总是将标签设置为 Detected - NOT RUNNING
  • 哦,是的,我已经删除了我明白你的意思,但这对我的问题没有影响......

标签: vb.net order-of-execution


【解决方案1】:

我自己解决了这个问题,见下文。

Private Sub detectExchange()

    Dim exchange As New ServiceController("Microsoft Exchange Service Host")

    Try
        If exchange.Status.Equals(ServiceControllerStatus.Running) Or exchange.Status.Equals(ServiceControllerStatus.StartPending) Then
            Label9.Text = "Detected - Running"

        Else

        End If
        If exchange.Status.Equals(ServiceControllerStatus.Stopped) Or exchange.Status.Equals(ServiceControllerStatus.StopPending) Then
            Label9.Text = "Detected - NOT RUNNING"
        End If

    Catch ex As InvalidOperationException
        Label9.ForeColor = Color.Red
        Label9.Text = "NOT INSTALLED"

    End Try

End Sub

Private Sub detectADUC()
    Dim aduc As New ServiceController("Active Directory Domain Services")
    Try
        If aduc.Status.Equals(ServiceControllerStatus.Running) Or aduc.Status.Equals(ServiceControllerStatus.StartPending) Then
            Label10.Text = "Detected - Running"

        Else

        End If
        If aduc.Status.Equals(ServiceControllerStatus.Stopped) Or aduc.Status.Equals(ServiceControllerStatus.StopPending) Then
            Label10.Text = "Detected - NOT RUNNING"
        End If

    Catch ex As InvalidOperationException
        Label10.ForeColor = Color.Red
        Label10.Text = "NOT INSTALLED"

    End Try
End Sub

【讨论】:

    猜你喜欢
    • 2013-04-11
    • 2018-10-02
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多