的Process.Start阻止 [英] Process.Start is blocking

查看:145
本文介绍了的Process.Start阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话的Process.Start,但它会阻止当前线程

I'm calling Process.Start, but it blocks the current thread.

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}.");
}



即使当处理被关闭,将码不再响应。

Even when the process is closed, the code doesn't respond anymore.

不过的Process.Start是真的要阻止?这是怎么回事?

But Process.Start is really supposed to block? What's going on?

(这个过程正常启动)

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...");
        }
    }
}



控制台输出是:

Console output is:

---按ENTER键停止服务---
起动过程。

--- Press ENTER to stop service --- Starting process.

发现:

[STAThread]

使的Process.Start阻塞。我读 STAThread和多线程,但我不能用的Process.Start行为联系起来的概念。

Makes the Process.Start blocking. I read STAThread and Multithreading, but I cannot link the concepts with Process.Start behavior.

据我所知,STAThread是的需要的由Windows.Form。 ?如何使用Windows.Form时解决这一问题。

AFAIK, STAThread is required by Windows.Form. How to workaround this problem when using Windows.Form?

新闻为地狱:

如果我重建我的应用程序,在第一时间我正确运行的应用程序的工作,但如果我停止调试并重新启动IY,问题araise。

If I rebuild my application, the first time I run application work correctly, but if I stop debugging and restart iy again, the problem araise.

当应用程序没有调试器执行不提出的问题。

The problem is not raised when application is executed without the debugger.

推荐答案

没有,的Process.Start 不等待子进程完成......否则你将无法使用的功能,比如重定向的I / O。

No, Process.Start doesn't wait for the child process to complete... otherwise you wouldn't be able to use features like redirected 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");
    }
}

这版画你看,我仍然运行对我的盒子没有问题 - 是什么?它在你的盒子做

This prints "See, I'm still running" with no problems on my box - what's it doing on your box?

这篇关于的Process.Start阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆