views:

59

answers:

2

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

A: 

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

REA_ANDREW
I don't know enough to translate this to VB, but thanks for the answer :)
danwellman
+2  A: 

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();
            }
dsolimano
Thanks :DI used the following code in the end to get it to do what I want:Dim proc As New Processproc.StartInfo.FileName = "C:\\mybat.bat"proc.StartInfo.WindowStyle = ProcessWindowStyle.Hiddenproc.StartInfo.CreateNoWindow = Trueproc.Start()proc.WaitForExit()proc.Close()
danwellman