VB.NET Marquee 进度直到进程退出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26103799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET Marquee Progress Until Process Exits
提问by JuliusPIV
While I have someVBScript experience, this is my first attempt at creating a very simple VB.NET (Windows Forms Application) wrapper for a command line application. Please be kind!
虽然我有一些VBScript 经验,但这是我第一次尝试为命令行应用程序创建一个非常简单的 VB.NET(Windows 窗体应用程序)包装器。请善待!
I have a very simple GUI with two buttons that both do an action and I'd like to show a marquee progress bar until the action (read: the process) is complete (read: exits).
我有一个非常简单的 GUI,有两个按钮,它们都执行一个操作,我想显示一个选取框进度条,直到操作(读取:过程)完成(读取:退出)。
The 'save' button does this:
“保存”按钮执行以下操作:
Dim SaveEXE As Process = Process.Start("save.exe", "/whatever /arguments")
From there I'm starting the marquee progress bar:
从那里我开始选取框进度条:
ProgressBar1.Style = ProgressBarStyle.Marquee
ProgressBar1.MarqueeAnimationSpeed = 60
ProgressBar1.Refresh()
I thought I could use SaveEXE.WaitForExit()but the Marquee starts, then stops in the middle until the process exits. Not very useful for those watching; they'll think it hung.
我以为我可以使用SaveEXE.WaitForExit()但是 Marquee 启动,然后在中间停止,直到进程退出。对于观看的人来说不是很有用;他们会认为它挂了。
I thought maybe I could do something like this but that causes my VB.Net app to crash
我想也许我可以做这样的事情,但这会导致我的 VB.Net 应用程序崩溃
Do
ProgressBar1.Style = ProgressBarStyle.Marquee
ProgressBar1.MarqueeAnimationSpeed = 60
ProgressBar1.Refresh()
Loop Until SaveEXE.ExitCode = 0
ProgressBar1.MarqueeAnimationSpeed = 60
ProgressBar1.Refresh()
I'm not entirely sure what needs to be done, short of getting some formal training.
我不完全确定需要做什么,除了接受一些正式培训。
回答by Jens
You can use the new Async/Await Feature of .NET 4.5 for this:
为此,您可以使用 .NET 4.5 的新 Async/Await 功能:
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 Asynckeyword in the declaration of the RunProcesssub and the Awaitkeyword.
注意sub 和关键字Async声明中RunProcess的Await关键字。
You run the WaitForExitin another thread and by using Awaitthe 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.
您WaitForExit在另一个线程中运行 ,并且Await只要任务需要完成,使用该应用程序基本上就会在这一行停止。
然而,这同时也使您的 GUI 保持响应。对于示例,我只显示进度条(之前它是不可见的)并在任务完成后隐藏它。
This also avoids any Application.DoEventshocus pocus.
这也避免了任何Application.DoEvents欺骗性的欺骗。

