【问题标题】:How can i know when an Application.exe is running and when is closed in VB.Net?我如何知道 Application.exe 何时运行以及何时在 VB.Net 中关闭?
【发布时间】:2020-01-09 21:27:57
【问题描述】:

我目前正在开发一个应用程序,该应用程序可以通过登录名打开 program.exe,但我需要知道该应用程序何时关闭。也许我会监控那个程序的进程,但我不知道该怎么做。

我如何知道应用程序何时运行以及何时关闭?

【问题讨论】:

  • 您可以设置Process.EnableRaisingEvents = true并订阅Exited事件。如果Process有Window,也可以使用UI Automation,设置一个WindowPattern.WindowClosedEvent
  • 如果进程有窗口也表示控制台窗口。
  • 就个人而言,如果进程将很快退出,我倾向于考虑调用WaitForExit,如果它会很快退出,则测试HasExited,否则处理Exited

标签: windows vb.net cmd process


【解决方案1】:

您可以查看Process.HasExited

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim p = Process.Start("notepad.exe")

    While Not p.HasExited
        Await Task.Delay(100)
    End While

    MessageBox.Show("Closed")
End Sub

【讨论】:

  • 你在HasExited 部分击败了我,为你+1!
  • @Marco History 将讲述一个不同的故事,因为您编辑得如此之快 :)
【解决方案2】:

你可以使用:

Dim proc = Process.Start("full path of your exe here")
proc.WaitForExit()

第一行运行应用程序,而第二行等待应用程序完成。

或者,如果您想要连续检查

Private Async Sub RunExeAndWait()
    Dim proc = Process.Start("full path of your exe here")

    While Not proc.HasExited
        Await Task.Delay(1000)
    End While
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2020-10-14
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多