简体   繁体   中英

How do I supress dialogs from an exe that is executed using System.Diagnostic.Process?

I'm using a System.Diagnostic.Process to call another .exe. I'm creating an application which purpose is to run on a remote server. This .exe sometimes crashes and there's a message popup stopping the whole process -> [Application Name] has encountered a problem and is forced to close. I'm trying to find a way to make my c# program ignore this popup and continue executing.

是否无法通过调用.exe来修复错误?

In that case when your process does not work properly you will see a popup thats windows default but if you want to know that process ran successfully or not then this code will work for you

Subscribe to the Process.Exited event and then check Process.ExitCode:

 public void StartProcess()
{
 p.StartInfo.FileName = BasePath;
 p.StartInfo.Arguments = args;
 p.Start();
 p.Exited += new EventHandler(Process_Exited);
}

void Process_Exited(object sender, EventArgs e)
{
var p = sender as Process;
if (p.ExitCode != 0)
    MessageBox.Show(string.Format("Process failed: ExitCode = {0}", p.ExitCode));
}

IMHO the cleanest option to run such a buggy exe is to start it giving to your process debug privileges over it (you probably have to call explicitly CreateProcess via P/Invoke with the DEBUG_PROCESS flag in the process creation flags) and then have a thread process the debug events provided by WaitForDebugEvent ; whenever you get an unhandled, last-chance exception you can notify the main thread, kill the child process (thus avoiding the default Windows exception handler) and restart it if necessary, in all other cases just call ContinueDebugEvent to let the program run normally.

Notice that the pointer to the "event structure" provided by WaitForDebugEvent could be tricky to work with in C#.


edit : fortunately it seems that someone made a nice managed wrapper for the native debugging API, see here .

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