我正在调用Process.Start,但它阻塞了当前线程。
pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe");
// Start process
mProcess = new Process();
mProcess.StartInfo = pInfo;
if (mProcess.Start() == false) {
Trace.TraceError("Unable to run process {0}.");
}即使当进程关闭时,代码也不再响应。
但是Process.Start真的应该阻止吗?到底怎么回事?
(进程正确启动)
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
namespace Test
{
class Test
{
[STAThread]
public static void Main()
{
Thread ServerThread = new Thread(AccepterThread);
ServerThread.Start();
Console.WriteLine (" --- Press ENTER to stop service ---");
while (Console.Read() < 0) { Application.DoEvents(); }
Console.WriteLine("Done.");
}
public static void AccepterThread(object data)
{
bool accepted = false;
while (true) {
if (accepted == false) {
Thread hThread = new Thread(HandlerThread);
accepted = true;
hThread.Start();
} else
Thread.Sleep(100);
}
}
public static void HandlerThread(object data)
{
ProcessStartInfo pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe");
Console.WriteLine("Starting process.");
// Start process
Process mProcess = new Process();
mProcess.StartInfo = pInfo;
if (mProcess.Start() == false) {
Console.WriteLine("Unable to run process.");
}
Console.WriteLine("Still living...");
}
}
}控制台输出是:
--按ENTER键停止服务--启动过程
找到它:
STAThread
使Process.Start阻塞。我读过STAThread与多线程,但我不能将这些概念与Process.Start行为联系起来。
STAThread是Windows.Form的要求。如何在使用Windows.Form时解决这个问题?
该死的新闻:
如果我重新构建我的应用程序,我运行应用程序的第一次时间将正确工作,但是如果我停止调试并再次重新启动iy,问题就会出现。
在没有调试器的情况下执行应用程序时不会引发此问题。
发布于 2010-06-19 19:23:49
不,Process.Start不等待子进程完成.否则,您将无法使用像重定向I/O这样的功能。
示例控制台应用程序:
using System;
using System.Diagnostics;
public class Test
{
static void Main()
{
Process p = new Process {
StartInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe")
};
p.Start();
Console.WriteLine("See, I'm still running");
}
}上面印着“看,我还在跑”,盒子上没有问题--它在你的盒子上做什么?
发布于 2012-09-09 00:15:46
创建一个ProcessStartInfo并将UseShellExecute设置为false (默认值为true)。您的代码应该是:
pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe");
pInfo.UseShellExecute = false;
// Start process
mProcess = new Process();
mProcess.StartInfo = pInfo;
if (mProcess.Start() == false) {
Trace.TraceError("Unable to run process {0}.");
}我遇到了同样的问题,启动可执行文件,直接从可执行文件创建流程,解决了这个问题。
发布于 2016-06-15 20:23:08
在WinForms应用程序中,我经历了与最初的海报相同的阻塞行为,因此我创建了下面的控制台应用程序,以简化对此行为的测试。
Jon的示例使用记事本,正常加载只需几毫秒,因此线程块可能会被忽略。我试图启动Excel,它通常要花费更长的时间。
using System;
using System.Diagnostics;
using static System.Console;
using System.Threading;
class Program {
static void Main(string[] args) {
WriteLine("About to start process...");
//Toggle which method is commented out:
//StartWithPath(); //Blocking
//StartWithInfo(); //Blocking
StartInNewThread(); //Not blocking
WriteLine("Process started!");
Read();
}
static void StartWithPath() {
Process.Start(TestPath);
}
static void StartWithInfo() {
var p = new Process { StartInfo = new ProcessStartInfo(TestPath) };
p.Start();
}
static void StartInNewThread() {
var t = new Thread(() => StartWithPath());
t.Start();
}
static string TestPath =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
"\\test.xlsx";
}对StartWithPath和StartWithInfo的调用会阻塞控制台应用程序中的线程。直到Excel启动屏幕关闭,主窗口打开后,我的控制台才会显示“已启动的进程”。

StartInNewThread将立即在控制台上显示这两条消息,而Excel的启动屏幕仍处于打开状态。
https://stackoverflow.com/questions/3076911
复制相似问题