【问题标题】:capture exception from running process从正在运行的进程中捕获异常
【发布时间】:2013-05-27 06:11:19
【问题描述】:

我正在运行一个 EXE 文件进程,它将始终在后台运行并监听键盘事件,我使用以下代码启动 EXE 文件:

            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = exeFile;
            startInfo.Arguments = args;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;

            Process processTmp = new Process();
            processTmp.StartInfo = startInfo;
            processTmp.EnableRaisingEvents = true;

            try
            {
                _keyboardListener = processTmp;
                _keyboardListener.Start();
            }
            catch (Exception)
            {
            }

然后它会向我的应用程序发送一些输入,但有时它会抛出异常,并且进程将停止正常工作,但是我如何查看进程是否抛出了异常?当我测量退出代码时,会抛出一个异常,告诉我需要停止进程才能获取退出代码。

【问题讨论】:

    标签: c# process


    【解决方案1】:

    既然你想捕捉你的异常,你可以在你的catch块中使用

           try
            {
                _keyboardListener = processTmp;
                _keyboardListener.Start();
            }
            catch (Exception ex)
            {
    
            MessageBox.Show(ex.message);
    
               // OR
              //You can write to any text file using StreamWriter
    
            }
    

    希望对你有帮助

    【讨论】:

    • 问题是......它开始正常......但它可能需要 5-10 分钟才能结束。
    猜你喜欢
    • 1970-01-01
    • 2012-07-23
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    相关资源
    最近更新 更多