Reputation: 9253
Ok, I'm making a very basic vb.net winforms app, essentially you can drag files into it, and it then uses a batch file to process the files.
It's pretty simple and everything is going to plan so far, it accepts the right files, it uses the batch file to process them and the batch file does what it is supposed to.
The only problem is that I don't know how to hook into the Exited
event that can/should be raised by the batch file process when the process completes...
I want the DOS window of the batch file to remain hidden while it is running, so I have used ProcessStartInfo
to specify the batch file, then set the WindowStyle
property of the process to ProcessWindowStyle.Minimised
, then used System.Diagnostics.Process.Start(myBatch)
to start the process at the appropriate moment.
This is fine, it works and does what I want. However, the only way to tell when a process ends is to use the Exited
event. But the Exited
event apparently only works with a Process
not a ProcessStartInfo
. I could switch to use Process
instead but then I couldn't (AFAIK) run the DOS window minimised...
Is there a way around this? I've only been writing .net for a few days. This is how I'm running the batch file:
Dim myBatch As New ProcessStartInfo("C:\\batchFiles\\test.bat")
myBatch.WindowStyle = ProcessWindowStyle.Minimized
system.Diagnostics.Process.Start(myBatch)
Any ideas?
Thanks
Upvotes: 0
Views: 1553
Reputation: 27322
From the documentation: This event can occur only if the value of the EnableRaisingEvents
property is true.
So the following should work:
Dim procStart As New ProcessStartInfo
Dim WithEvents proc As New Process
Private Sub Button21_Click(sender As System.Object, e As System.EventArgs) Handles Button21.Click
procStart.FileName = "C:\PTL\Bin\xxxxxx.exe"
proc.StartInfo = procStart
proc.EnableRaisingEvents = True
proc.Start()
End Sub
Private Sub proc_Exited(sender As Object, e As System.EventArgs) Handles proc.Exited
Debug.WriteLine("Process Ended " + proc.ExitCode.ToString + " " + DateTime.Now.ToString)
End Sub
Upvotes: 0
Reputation: 8986
Try creating a process object and setting the StartInfo property. You can then call WaitForExit instead of waiting for the event. EG:
using(var process = new Process
{
StartInfo =
new ProcessStartInfo("Foo.exe")
{WindowStyle = ProcessWindowStyle.Minimized}
})
{
process.Start();
process.WaitForExit();
}
Upvotes: 2
Reputation: 10764
Not sure of the syntax in VB but I am almost sure that what you have to do is actually use the WIN API inline with managed code, and then you can use the MainWindowHandle of the Process Object.
[DllImport("User32")]
private static extern int ShowWindow(int hwnd, int nCmdShow);
The commands it takes, I would recommend reference to the win api library for this method. But what you want to do I would think is very feasible with the interop.
Andrew
Upvotes: 0