MadsTheMan
MadsTheMan

Reputation: 703

Executing multiple bat files with Visual basics

I'm working on a program created with Visual Studio 2013. The program does a few things and I'm nearly complete, but one last issue appears.

Within the program I have three buttons. One "Force restart", "manual start" and one "force stop". The "force stop" stops a bunch of programs, and the "force restart" stops them and then starts them again. The "manual start" starts all the programs. I will use "manual start" as example further down.

What happens behind these buttons is that it launches a bunch of bat-files that does the job. The batfiles contains tasskill /f /im program.exe and start "c:\program.exe". Then a second with timeout and exits.

The issue:

So far so good. The issue is that when the batch starts a program, VB program doesnt move on the the next bat file. It leaves a cmd.exe running. Even tho I have exit in the batch. Now If I'd go in and manually close the program or cmd.exe, then it would start on the next bat file.

It basically goes like this now: VB button -> batch starts -> batch runs program -> batch doesn't close AKA VB doesn't move to the next batch.

It should be like this: VB button -> batch starts -> batch runs program -> batch exits -> next batch starts -> batch runs program -> batch exits ->...

Here is what I got so far in that section of VB script:

Private Sub btnManualStart_Click(sender As Object, e As EventArgs) Handles btnManualStart.Click
If MessageBox.Show("Do you want to manually start all programs and services?", "OBS", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
        Timer2.Start()
        Dim FileNum As Integer = FreeFile()
        FileClose(FileNum)
        TextBox1.Text &= Environment.NewLine & TimeOfDay & (" Manual start made")
        Dim shell
        shell = CreateObject("wscript.shell")
        shell.run("C:\RestartApps\Scripts\Start_program1.bat", 0, True)
        shell.run("C:\RestartApps\Scripts\Start_program2.bat", 0, True)
        shell.run("C:\RestartApps\Scripts\Start_program3.bat", 0, True)
        shell.run("C:\RestartApps\Scripts\Start_program4.bat", 0, True)
        shell = Nothing
end if

Hopefully this was understandable.

Upvotes: 1

Views: 1128

Answers (2)

JosefZ
JosefZ

Reputation: 30103

You need to improve start used in your batch script(s) as follows:

start "" /B "c:\program.exe"

Start command: start a program, command or batch script (opens in a new window). Note:

  • "" always include a TITLE this can be a simple string or just a pair of empty quotes "";
  • /B start application without creating a new window.

Another approach: use call instead of start "" as follows:

call "c:\program.exe"

Upvotes: 1

Sérgio Ribeiro
Sérgio Ribeiro

Reputation: 104

I do not know exactly what your batches are doing, but it seems to me that you can simply use the Shell shortcut:

Dim commands As String() = {
    "C:\RestartApps\Scripts\Start_program1.bat",
    "C:\RestartApps\Scripts\Start_program2.bat",
    "C:\RestartApps\Scripts\Start_program3.bat",
    "C:\RestartApps\Scripts\Start_program4.bat"
}

For Each cmd As String In commands
    Shell(cmd, AppWinStyle.Hide, False)
Next

Make sure that you set the third argument to FALSE on the overload (String,AppWinStyle,Boolean). This boolean ensures that the execution is set to "fire and forget". (which is the same as the one you're already passing, as TRUE (will wait for exit code)).

EDIT: Changed the AppWinStyle to Hide, which will run your batches silently

Upvotes: 2

Related Questions