VB.NET Marquee Progress Until Process Exits

后端 未结 1 431
梦毁少年i
梦毁少年i 2021-01-23 16:23

While I have some VBScript experience, this is my first attempt at creating a very simple VB.NET (Windows Forms Application) wrapper for a command line application. Pl

1条回答
  •  北海茫月
    2021-01-23 16:44

    You can use the new Async/Await Feature of .NET 4.5 for this:

    Public Class Form1
        Private Async Sub RunProcess()
            ProgressBar1.Visible = True
            Dim p As Process = Process.Start("C:\test\test.exe")
            Await Task.Run(Sub() p.WaitForExit())
            ProgressBar1.Visible = False
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            RunProcess()
        End Sub
    End Class
    

    Note the Async keyword in the declaration of the RunProcess sub and the Await keyword.

    You run the WaitForExit in another thread and by using Await the application basically stops at this line as long as the task takes to complete.
    This however also keeps your GUI reponsive meanwhile. For the example I just show the progressbar (it is invisible before) and hide it once the task is complete.

    This also avoids any Application.DoEvents hocus pocus.

    0 讨论(0)
提交回复
热议问题