B C B C - 1 year ago 156
C# Question

How do I use wait for exit when using C# to use a vbs file to run bat file?

How do I use wait for exit when using C# to use a vbs file to run bat file?

Currently, it does not wait until the vbs is done. I have added the vbs code.



private string SetBackUpFiles(string vbsFileName, CheckBox BackUpCheckBox)
{
Process proc = null;
string BackUpCompleted;
BackUpCompleted = "Back up function error";
try
{
proc = new Process();
proc.StartInfo.WorkingDirectory = vbsDir;
proc.StartInfo.FileName = vbsFileName;
proc.StartInfo.CreateNoWindow = false;
proc.EnableRaisingEvents = true;
proc.Start();

proc.WaitForExit();

BackUpCompleted = "Backup Completed!";
BackUpCheckBox.Checked = true;
BackUpCheckBox.Visible = true;

}
catch (Exception ex)
{
BackUpCompleted = (ex.StackTrace.ToString());
}
return BackUpCompleted;
}
--------VBS script-----
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c LayoutsBackup.bat"
oShell.Run strArgs, 0, false

Answer Source

You are executing an other command inside that VBS script that it runs an other cmd. So the VBS script it just calls the cmd command and it exits without checking if the shell run has finished. Try to change your VBScript like this:

oShell.Run strArgs, 0, true

Run Documentation

.Run

Run an external Command.

Syntax

objShell.Run (strCommand, [intWindowStyle], [bWaitOnReturn])

Last parameter says the Run to wait until finish.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download