挂过程时使用.NET的Process.Start运行 - 有什么不对? [英] Hanging process when run with .NET Process.Start -- what's wrong?

查看:231
本文介绍了挂过程时使用.NET的Process.Start运行 - 有什么不对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了周围svn.exe一个快速和肮脏的包装来获取一些内容,并用它做什么,但对于某些输入偶尔和可重复挂起,将无法完成。例如,一个调​​用的是SVN列表:

I wrote a quick and dirty wrapper around svn.exe to retrieve some content and do something with it, but for certain inputs it occasionally and reproducibly hangs and won't finish. For example, one call is to svn list:

svn list "http://myserver:84/svn/Documents/Instruments/" --xml  --no-auth-cache --username myuser --password mypassword

此命令行运行良好时,我只是做它从一个命令shell,但它在我的应用程序挂起。我的C#code运行是这样的:

This command line runs fine when I just do it from a command shell, but it hangs in my app. My c# code to run this is:

string cmd = "svn.exe";
string arguments = "list \"http://myserver:84/svn/Documents/Instruments/\" --xml  --no-auth-cache --username myuser --password mypassword";
int ms = 5000;
ProcessStartInfo psi = new ProcessStartInfo(cmd);
psi.Arguments = arguments;
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.UseShellExecute = false;
Process proc = Process.Start(psi);
StreamReader output = new StreamReader(proc.StandardOutput.BaseStream, Encoding.UTF8);

proc.WaitForExit(ms);
if (proc.HasExited)
{
    return output.ReadToEnd();
}

这需要完整的5000毫秒,并永远不会完成。延长时间并不能帮助。在另一项命令提示符,运行瞬间,所以我pretty确保它无关不足的等待时间。对于其他的投入,但是,这似乎很好地工作。

This takes the full 5000 ms and never finishes. Extending the time doesn't help. In a separate command prompt, it runs instantly, so I'm pretty sure it's unrelated to an insufficient waiting time. For other inputs, however, this seems to work fine.

我也试过在这里运行一个单独的cmd.exe(其中EXE是svn.exe和args是原来的ARG字符串),但仍挂发生:

I also tried running a separate cmd.exe here (where exe is svn.exe and args is the original arg string), but the hang still occurred:

string cmd = "cmd";
string arguments = "/S /C \"" + exe + " " + args + "\"";

我能怎么在这里搞砸了,我怎么可以调试这个外部进程东西?

What could I be screwing up here, and how can I debug this external process stuff?

编辑:

我刚才让周围,以解决该问题。 MUCHO感谢乔恩斯基特对他的建议,这的确伟大工程。我对我的处理这一点,虽然方法的另一个问题,因为我是一个多线程的新手。我想提高任何明显的缺陷或任何其它哑建议。我结束了创建包含stdout流,一个StringBuilder来保存输出,和一个标志时,它的完成讲一个小班。然后我用ThreadPool.QueueUserWorkItem在我的类的实例传递:

I'm just now getting around to addressing this. Mucho thanks to Jon Skeet for his suggestion, which indeed works great. I have another question about my method of handling this, though, since I'm a multi-threaded novice. I'd like suggestions on improving any glaring deficiencies or anything otherwise dumb. I ended up creating a small class that contains the stdout stream, a StringBuilder to hold the output, and a flag to tell when it's finished. Then I used ThreadPool.QueueUserWorkItem and passed in an instance of my class:

ProcessBufferHandler bufferHandler = new ProcessBufferHandler(proc.StandardOutput.BaseStream,
                                                                          Encoding.UTF8);
ThreadPool.QueueUserWorkItem(ProcessStream, bufferHandler);

proc.WaitForExit(ms);
if (proc.HasExited)
{
    bufferHandler.Stop();
    return bufferHandler.ReadToEnd();
}

......然后......

... and ...

private class ProcessBufferHandler
{
    public Stream stream;
    public StringBuilder sb;
    public Encoding encoding;
    public State state;

    public enum State
    {
        Running,
        Stopped
    }

    public ProcessBufferHandler(Stream stream, Encoding encoding)
    {
        this.stream = stream;
        this.sb = new StringBuilder();
        this.encoding = encoding;
        state = State.Running;
    }
    public void ProcessBuffer()
    {
        sb.Append(new StreamReader(stream, encoding).ReadToEnd());
    }

    public string ReadToEnd()
    {
        return sb.ToString();
    }

    public void Stop()
    {
        state = State.Stopped;
    }
}

这似乎是工作,但我怀疑,这是最好的方式。这是合理的?我能做些什么来改善呢?

This seems to work, but I'm doubtful that this is the best way. Is this reasonable? And what can I do to improve it?

推荐答案

一个标准的问题:这个过程可能会等着你去阅读它的输出。创建一个单独的线程,而你等待它退出,从它的标准输出读取。这是一个有点痛,但很可能是问题所在。

One standard issue: the process could be waiting for you to read its output. Create a separate thread to read from its standard output while you're waiting for it to exit. It's a bit of a pain, but that may well be the problem.

这篇关于挂过程时使用.NET的Process.Start运行 - 有什么不对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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