c#启动程序时使用异步读取输出避免假死

某次调用某个exe,在cmd命令中调用正常,但是一旦使用代码调用则直接假死。

后来了解到原来是输出流阻塞导致的假死,这个时候只需要使用异步读取输出就可以了,直接上代码:

cs 复制代码
        public static bool ExecuteFileConvertHelper(string sPara)
        {
            try
            {
                // 获取当前程序所在的目录
                string exeDir = AppDomain.CurrentDomain.BaseDirectory;
                // 拼接 FileConvert.exe 的完整路径
                string exePath = Path.Combine(exeDir, "FileConvert.exe");

                // 检查文件是否存在
                if (!File.Exists(exePath))
                {
                    LibLogger.LogManager.GetManager().WriteCommon($"FileConvert.exe not found at: {exePath}");
                    return false;
                }

                // 创建一个新的进程启动信息
                ProcessStartInfo startInfo = new ProcessStartInfo
                {
                    FileName = exePath,
                    Arguments = sPara,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    UseShellExecute = false,
                    CreateNoWindow = true // 不创建窗口
                };

                // 启动进程
                using (Process process = Process.Start(startInfo))
                {
                    // 确保进程不是 null
                    if (process == null)
                    {
                        return false;
                    }

                    // 异步读取标准输出
                    process.OutputDataReceived += (sender, e) =>
                    {
                        if (!string.IsNullOrEmpty(e.Data))
                        {
                            // 可以在这里处理输出数据,例如记录日志
                            //LibLogger.LogManager.GetManager().WriteCommon(e.Data);
                        }
                    };

                    // 异步读取标准错误
                    process.ErrorDataReceived += (sender, e) =>
                    {
                        if (!string.IsNullOrEmpty(e.Data))
                        {
                            // 可以在这里处理错误数据,例如记录错误日志
                            //LibLogger.LogManager.GetManager().WriteCommon(e.Data);
                        }
                    };

                    // 开始异步读取
                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    // 等待进程退出
                    process.WaitForExit();

                    // 检查退出代码
                    if (process.ExitCode == 0)
                    {
                        // 成功执行
                        return true;
                    }
                    else
                    {
                        // 执行失败
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                // 可以在这里记录异常日志
                LibLogger.LogManager.GetManager().WriteCommon(ex.Message);
                return false;
            }
    }

谨此纪念。

相关推荐
乎里陈4 分钟前
【JAVA】十三、基础知识“接口”精细讲解!(三)(新手友好版~)
java·object·equals·tostring·hashcode·深拷贝浅拷贝·clonable
weixin_428498499 分钟前
在Star-CCM+中实现UDF并引用场数据和网格数据
java·前端
工具罗某人10 分钟前
IDEA 2024 版本配置热部署
java·ide·intellij-idea
王天华帅哥14 分钟前
分布式id的两大门派!时钟回拨问题的解决方案!
java
半青年1 小时前
基于Qt开发的http/https客户端
java·c++·qt·网络协议·http·https·信息与通信
阿蒙Amon1 小时前
DevExpress&WinForms-AlertControl-使用教程
c#·devexpress·winforms
冠位巴萨辛山の翁1 小时前
Maven
java·maven
小猫猫改bug1 小时前
threejs 添加css3d标签 vue3
前端·javascript·css3
前端小巷子1 小时前
CSS3 过渡与动画
前端·css·css3
m0_zj1 小时前
58.[前端开发-前端工程化]Day05-webpack-Git安装-配置-Git命令
前端·webpack·node.js