简体   繁体   中英

Run Batch file in Visual Basic

I have few questions about running Batch file within Visual Basic.

There is a button in windows form which built on VB, and there have click event for the button.

Example code:

    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

Above code means when user click button, a hidden batch file (.bat) running back end.

My issue is, while this btn clicked by user, the whole application window will be freeze, even can not minimize to task bar.

Is there any way I can run the batch file, get some return value let me know it completed? or which status it is? Because I also want to show the progress bar for it.

Any ideas? Thanks in advance.

Any ideas?

Yes, try to don't depend on Batch while you are programming using .NET Classes, simply as that.

Anyways with the usage of process2.WaitForExit() you're waiting for process to exit, that's why program freezes, program thread is waiting.

An easy solution is this:

    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. 

Exists better ways to do this (multi threading, Backgroundworker) but since you are new in this language and depending on a simple language as Batch then I will recommend you to use easy things, but is a great idea to introduce in multi-threading techniques.

PS: Forgive my English please.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM