简体   繁体   中英

Suspending main process in C#

I am developing an application in C#. As part of an error condition, I would like to bring up a file in Notepad for the user to edit (while pausing the main process). I am able to bring the file up using Process.Start. What I can't figure out is how to pause the main application until the user saves or quits out of the notepad file. Is there something other than Process.start that I could use that would launch the Notepad.exe in the main process or some other trick I can use. I feel like there is an easy solution to it that I am overlooking. Thanks in advance.

You could use Process.WaitForExit to block the main program until Notepad is closed.

Making it block until the file is saved or closed would be a bit trickier.

One option here would be to have a ManualResetEvent, and wait on it. You could then listen for Process.Exited (after setting Process.EnableRaisingEvents ), and use this to call Set() on the wait handle. This would handle allowing your code to continue when the process exits.

For handling the file being saved, a separate FileSystemWatcher could watch for the save (file change) on the file itself. If the file saves/changes, you could continue on by calling Set().

I believe you should be able to do something like this.

var notePad = new Process { StartInfo = { FileName = "notepad.exe", Arguments = "newfile.txt" } };
        notePad.Start();

        while(!notePad.HasExited)
        {

        }

        MessageBox.Show("Done.");

The "Done." message box shouldn't show up until you close notepad. Then again, the answer above mine is less hackish.

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