Reputation: 3993
I have a C# WinForms program thats starts another Process. The program then waits until the Process has finished.
Currently I use Process.WaitForExit(), but this means that while my program waits for the Process to end, it doesn't repaint and "looks" like it's not responding.
Is there any way for the Window of the Process that my program starts, be modal to my main form (i.e. you can't switch back to my program & the window repaints)?
Basically I want to do something like Form.ShowDialog(), except using the Process's Window as the Form to be shown as a dialog.
Upvotes: 4
Views: 5128
Reputation: 1
Another way that is a little non-standard but it works. Define a variable ProcessActive. On each of the controls visible in the app, check the variable and if set, don't process the controls event. Before launching the process, set the ProcessActive variable to True and launch the process with events. Define the Exited event on the process so that it sets the ProcessActive to False. They can switch back to the launching application and everything looks good. It just won't do anything until the process has exited. You might want to set the cursor to the hourglass so they know that the app is busy.
Upvotes: 0
Reputation: 896
To the best of my knowledge, this isn't possible. You can use SetParent (unmanaged) to set the child / parent relationship, but that is as far as you can go. Note: I'm using a perl based program and the SetParent() routine doesn't seem to work as advertised.
Upvotes: 0
Reputation: 101150
Upvotes: 0
Reputation: 48696
You should definately create a new thread and from that thread, launch the application you are trying to launch. This will make your application responsive, even while the other application is running
Upvotes: 0
Reputation: 347276
You could create another form which you make modal. On that form load you would call Visible = false;
then you would start a thread which starts your process and waits for it to finish via the thread.
Then do a BeginInvoke(new MethodInvoker(delegate { Close(); } ));
from within the thread after the wait for complete finishes.
Upvotes: 0
Reputation: 137148
You can't prevent the user from switching back because you've spawned a separate process. As far as the operating system is concerned it's as if you'd started the second one via it's desktop icon (for example).
I think the best you can hope for is to disable the relevant menus/options when the second process is active. You'll need to keep polling to see if it's still alive, otherwise your main application will become unusable.
Another approach might be to minimize the main application which will keep it out of the way.
Upvotes: 1
Reputation: 3794
Why don't you start your process in a separate thread? That way, only your thread will wait. This way, your form will still respond.
Upvotes: 1
Reputation: 18387
Create Form, Show it modal. then use BackgroundWorker to start process and handle RunWorkerCompleted event to close the form.
Upvotes: 0