简体   繁体   中英

How to start a Process and make its main window modal to the starting process's window?

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 (ie 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.

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.

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.

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.

Create Form, Show it modal. then use BackgroundWorker to start process and handle RunWorkerCompleted event to close the form.

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.

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

  1. Use the process.Exited event to know when the application exits.
  2. Hook the Activated (think it's the correct name) event in you form. Use it to bring the other application to focus.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM