VB.NET Marquee 进度直到进程退出 [英] VB.NET Marquee Progress Until Process Exits

查看:51
本文介绍了VB.NET Marquee 进度直到进程退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我有一些 VBScript 经验,但这是我第一次尝试为命令行应用程序创建一个非常简单的 VB.NET(Windows 窗体应用程序)包装器.请善待!

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. Please be kind!

我有一个非常简单的 GUI,有两个按钮,它们都执行一个操作,我想显示一个选取框进度条,直到操作(读取:进程)完成(读取:退出).

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).

保存"按钮的作用是:

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()


我以为我可以使用 SaveEXE.WaitForExit() 但选取框启动,然后在中间停止,直到进程退出.对于观看的人来说不是很有用;他们会认为它挂了.


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.


我想也许我可以做这样的事情,但这会导致我的 VB.Net 应用程序崩溃


I thought maybe I could do something like this but that causes my VB.Net app to crash

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.

推荐答案

您可以为此使用 .NET 4.5 的新 Async/Await 功能:

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

注意 RunProcess 子声明中的 Async 关键字和 Await 关键字.

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

您在另一个线程中运行 WaitForExit 并通过使用 Await 应用程序基本上停止在这一行,只要任务需要完成.
然而,这同时也使您的 GUI 保持响应.例如,我只显示进度条(之前是不可见的)并在任务完成后隐藏它.

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.

这也避免了任何 Application.DoEvents 骗局.

This also avoids any Application.DoEvents hocus pocus.

这篇关于VB.NET Marquee 进度直到进程退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆