在Visual Basic中运行批处理文件

时间:2013-10-19 12:57:52

标签: vb.net batch-file process

关于在Visual Basic中运行批处理文件,我几乎没有问题。

在Windows窗体中有一个构建在VB上的按钮,按钮有点击事件。

示例代码:

    Dim pathtobatfile2 As String
    pathtobatfile2 = "C:\extract.bat" 
    ...............
    psi2.UseShellExecute = False
    Dim process2 As Process = process.Start(psi)
    process2.WaitForExit()
    'then do something after the bat file done

以上代码表示当用户单击按钮时,隐藏的批处理文件(.bat)运行结束。

我的问题是,当用户点击这个btn时,整个应用程序窗口会被冻结,甚至无法最小化到任务栏。

有什么方法可以运行批处理文件,获得一些返回值让我知道它已经完成了吗?或者是哪种状态?因为我也想显示它的进度条。

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

Any ideas?

是的,尝试在使用Batch类进行编程时不依赖于.NET,就像那样。

无论如何使用process2.WaitForExit()等待进程退出,这就是为什么程序冻结,程序线程正在等待。

这是一个简单的解决方案:

    Dim process2 As Process With ...
    ...

    process2.Start()
    While Not process2.HasExited
        Application.doevents()
    End While

    ' Here the process has exited,
    ' Then add your code here. 

存在更好的方法(多线程,背景工作者),但由于你是这种语言的新手,并且依赖于Batch之类的简单语言,我会建议你使用简单的东西,但这是一个好主意引入多线程技术。

PS:请原谅我的英语。