【发布时间】:2023-03-10 13:00:01
【问题描述】:
我有一个使用 backgroundWorker 和线程池运行的 C# 类 MyCommand。 MyCommand 使用 Process 类执行命令行可执行文件。 MyCommand 的实例,每个都在自己的线程中,以串行方式而不是并行方式运行。我只是想知道 MyCommand 中 Process.Start() 的执行是否阻塞了其他线程。
Process pProcess = new Process();
pProcess.StartInfo.FileName = "decrypt.exe";
pProcess.StartInfo.Arguments = "list of args" ;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string strOutput = pProcess.StandardOutput.ReadToEnd();
pProcess.WaitForExit();
【问题讨论】:
-
这些长时间运行的进程是否会引发致命异常?如果不是,我不会采用流程。查看 MSDN 上的任务并行库。
-
您的代码有问题。但是,如果不查看您的代码,我们就无法判断出了什么问题。
-
在这个过程中,我运行一个解密文件的可执行文件,所以每个线程解密一个不同的文件。解密程序正在完成。我正在打印它的输出。解密程序是从 c++ 源代码编译的。但根据其他回应,似乎可能与程序本身有关。因此,在我的表单中,我通过单击按钮启动后台工作程序,该按钮将执行到 DoWork 方法。在这种方法中,我使用以下文章中提供的示例使用线程池启动线程:msdn.microsoft.com/en-us/library/3dasc8as%28v=vs.80%29.aspx
-
在 MyCommand 我有这样的东西: Process pProcess = new Process(); pProcess.StartInfo.FileName = "解密.exe"; pProcess.StartInfo.Arguments = ... pProcess.StartInfo.UseShellExecute = false; pProcess.StartInfo.RedirectStandardOutput = true; pProcess.StartInfo.CreateNoWindow = true; pProcess.Start();字符串 strOutput = pProcess.StandardOutput.ReadToEnd(); pProcess.WaitForExit();
标签: c# multithreading process