用户界面-如何在VB中不冻结.. NET while Process.WaitForExit()



我有什么:

Dim test As Process = Process.Start("powershell.exe", "-executionpolicy
remotesigned -file C:temptest.ps1")
test.WaitForExit()

最后,当等待"test"退出时,我的GUI冻结了。如何使它光滑?

你可以使用BackgroundWorker在一个单独的线程上运行这段代码,目前你正在UI线程上运行它,这会导致它冻结。

这应该让你开始:

Private Sub startWorker()
    Dim powershellWorker As New BackgroundWorker
    AddHandler powershellWorker.DoWork, AddressOf doWork
    powershellWorker.RunWorkerAsync()
End Sub
Private Sub doWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    Dim test As Process = Process.Start("powershell.exe", "-executionpolicy remotesigned -file C:temptest.ps1")
    test.WaitForExit()
End Sub

test.WaitForExit()是一个阻塞方法,它会阻塞你的UI线程。尝试在并行任务中运行代码并等待

 Dim task As Task = Task.Run(Sub()
     Dim test As Process = Process.Start("powershell.exe", "-executionpolicy remotesigned -file C:temptest.ps1")
     Process.WaitForExit()
     Return test
  End Sub)
 Dim test2 As Process = Await task
 // some code that will execute after the process completes

你可能需要在外部方法声明中添加async关键字。

请注意,我是一个c#的人:-)

相关内容

  • 没有找到相关文章

最新更新