Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当StartInfo.RedirectStandardInput设置为true时,为什么StandardOutput.Read()会阻塞?_C#_.net_Process_Subprocess_Stdout - Fatal编程技术网

C# 当StartInfo.RedirectStandardInput设置为true时,为什么StandardOutput.Read()会阻塞?

C# 当StartInfo.RedirectStandardInput设置为true时,为什么StandardOutput.Read()会阻塞?,c#,.net,process,subprocess,stdout,C#,.net,Process,Subprocess,Stdout,关于Read(Char[],Int32,Int32)方法是否阻塞,我很难解读MSDN文档。我的理解是它不应该阻塞,但当我将RedirectStandardInput设置为true时,它似乎会阻塞 有没有人有这方面的经验;或者对我遇到的问题做些解释 这里的上下文是,我不想在读取标准输出之前等待完整的行(即带有行终止符),或者等待进程退出。另外,我不想使用回调我希望在进程写入时同步读取标准输出。 以下是我的代码的简化版本: string command = @"C:\flex_sdks\flex_s

关于Read(Char[],Int32,Int32)方法是否阻塞,我很难解读MSDN文档。我的理解是它不应该阻塞,但当我将RedirectStandardInput设置为true时,它似乎会阻塞

有没有人有这方面的经验;或者对我遇到的问题做些解释


这里的上下文是,我不想在读取标准输出之前等待完整的行(即带有行终止符),或者等待进程退出。另外,我不想使用回调我希望在进程写入时同步读取标准输出。

以下是我的代码的简化版本:

string command = @"C:\flex_sdks\flex_sdk_4.5.1.21328\bin\fcsh.exe";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = false; # <-- if I set this to true, then 
                                           # the program hangs on
                                           # p.StandardOutput.Read later on
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = command;
p.Start();

StringBuilder sb_stdout = new StringBuilder(1024);
char[] buffer = new char[64];
int nb_bytes_read;
while (true) {
    do {
        nb_bytes_read = p.StandardOutput.Read(buffer, 0, buffer.Length);
        sb_stdout.Append(new string(buffer, 0, nb_bytes_read));
    } while (nb_bytes_read > 0);
    if (sb_stdout.ToString().EndsWith("\n(fcsh) "))
        break;
    Thread.Sleep(20);
}
string命令=@“C:\flex\u sdks\flex\u sdk\u 4.5.1.21328\bin\fcsh.exe”;
过程p=新过程();
p、 StartInfo.UseShellExecute=false;
p、 StartInfo.RedirectStandardInput=false;#0);
if(sb_stdout.ToString().EndsWith(“\n(fcsh)”))
打破
睡眠(20);
}
更新 基于我(可能不好)的假设,即Process.StandardOutput在使用时会被破坏:

  • stdin被重定向;以及
  • 从stdout或stderr读取非终止行的内容时
我决定尝试直接使用Windows的API。我用这样的代码添加了一个答案;它运行良好(至少目前如此)

另一个更新
我用我现在使用的代码创建了一个数据库。

从您的帖子中不太清楚您所说的“我需要在流程写入后立即同步读取”。如果您需要即时反馈,您需要异步管理

伪代码:

同步管理:

string sOutput = process.StandardOutput.ReadToEnd();
process.WaitToExit();
/*subscribe to events in order to receive notification*/    
p.StartInfo.RedirectStandardInput = true;
p.OutputDataReceived += Subscription
异步管理:

string sOutput = process.StandardOutput.ReadToEnd();
process.WaitToExit();
/*subscribe to events in order to receive notification*/    
p.StartInfo.RedirectStandardInput = true;
p.OutputDataReceived += Subscription
之后,如果需要
p.WaitForExit(),如果您不在乎它何时完成,而只想从中获取数据,您甚至可以避免使用该行


希望这能有所帮助。

陈雷蒙最近在他的博客上谈到:


就在上个星期,我一直在和这个战斗。。。出于某种原因,除了Read()调用(ReadToEnd()不是我所需要的)之外的任何东西似乎都会阻塞并且永远不会返回。以下是我为最终让它“工作”所做的:

剪报1:

    private bool ThreadExited = true;
    private bool ExitThread = false;
    private void ReadThread()
    {
        while (!ExitThread)
        {
            string Output = "";

            int CharacterInt = myProcess.StandardOutput.Read();

            while (CharacterInt > 0)
            {
                char Character = (char)CharacterInt;
                Output += Character;

                var MyDelegate = new delegateUpdateText(UpdateText);
                Invoke(MyDelegate, Output);

                Output = "";
                CharacterInt = myProcess.StandardOutput.Read();
            }

            System.Threading.Thread.Yield();
        }

        ThreadExited = true;
    }
剪报2:

    private void InitializeProcess()
    {
        ThreadExited = true;
        ExitThread = true;

        while (!ThreadExited)
            System.Threading.Thread.Sleep(1000);

        ThreadExited = false;
        ExitThread = false;

        myProcess = new Process();

        ProcessStartInfo PSI = myProcess.StartInfo;

        PSI.FileName = @"cmd.exe";
        PSI.UseShellExecute = false;
        PSI.RedirectStandardError = false;
        PSI.RedirectStandardInput = true;
        PSI.RedirectStandardOutput = true;
        PSI.CreateNoWindow = false;
        PSI.ErrorDialog = true;


        myProcess.StartInfo = PSI;

        myProcess.Exited += new EventHandler(myProcess_Exited);

        myProcess.EnableRaisingEvents = false;


        myProcess.Start();

        ReadThreadThread = new System.Threading.Thread(ReadThread);
        ReadThreadThread.Start();
    }
    private System.Threading.Thread ReadThreadThread;

这最终对我起了作用。在我的例子中,我正在向文本框中写入文本,但这应该很容易修改为其他内容。但我所做的任何其他事情都会因阻塞而引起问题;出于某种原因,即使我使用反射来获取可用的字节数,调用ReadBlock()函数也会阻塞。我从来没有想到会让我满意。

在与Ben Voigt讨论了这一点之后,我决定在不使用System.Diagnostics.process的情况下实现与流程的通信。这就是我现在想到的,它工作得很好,每次都能始终如一地工作,没有任何东西阻塞或挂起

我之所以发布这篇文章,是因为这可能有助于任何需要读取stdout/stderr并在不使用System.Diagnostics.process的情况下向stdin写入某些已创建进程的人

const UInt32 STARTF_USESTDHANDLES = 0x00000100;
const int HANDLE_FLAG_INHERIT = 1;

struct PROCESS_INFORMATION
{
    public IntPtr hProcess;
    public IntPtr hThread;
    public uint dwProcessId;
    public uint dwThreadId;
}

struct STARTUPINFO
{
    public uint cb;
    public string lpReserved;
    public string lpDesktop;
    public string lpTitle;
    public uint dwX;
    public uint dwY;
    public uint dwXSize;
    public uint dwYSize;
    public uint dwXCountChars;
    public uint dwYCountChars;
    public uint dwFillAttribute;
    public uint dwFlags;
    public short wShowWindow;
    public short cbReserved2;
    public IntPtr lpReserved2;
    public IntPtr hStdInput;
    public IntPtr hStdOutput;
    public IntPtr hStdError;
}

struct SECURITY_ATTRIBUTES
{
    public int length;
    public IntPtr lpSecurityDescriptor;
    [MarshalAs(UnmanagedType.Bool)]
    public bool bInheritHandle;
}

[DllImport("kernel32.dll")]
static extern bool CreateProcess(string lpApplicationName,
                                 string lpCommandLine,
                                 IntPtr lpProcessAttributes,
                                 IntPtr lpThreadAttributes,
                                 bool bInheritHandles,
                                 uint dwCreationFlags,
                                 IntPtr lpEnvironment,
                                 string lpCurrentDirectory,
                                 ref STARTUPINFO lpStartupInfo,
                                 out PROCESS_INFORMATION lpProcessInformation);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr hObject);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CreatePipe(out IntPtr hReadPipe,
                              out IntPtr hWritePipe,
                              ref SECURITY_ATTRIBUTES lpPipeAttributes,
                              uint nSize);

[DllImport("kernel32", SetLastError = true)]
static extern unsafe bool ReadFile(IntPtr hFile,
                                   void* pBuffer,
                                   int NumberOfBytesToRead,
                                   int* pNumberOfBytesRead,
                                   IntPtr lpOverlapped);

[DllImport("kernel32.dll")]
static extern unsafe bool WriteFile(IntPtr hFile,
                                    void* pBuffer,
                                    int nNumberOfBytesToWrite,
                                    int* lpNumberOfBytesWritten,
                                    IntPtr lpOverlapped);

[DllImport("kernel32.dll")]
static extern bool SetHandleInformation(IntPtr hObject, int dwMask, uint dwFlags);

void OpenAndCloseFcsh()
{
    STARTUPINFO si = new STARTUPINFO();
    SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
    PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

    sa.bInheritHandle = true;
    sa.lpSecurityDescriptor = IntPtr.Zero;
    sa.length = Marshal.SizeOf(typeof(SECURITY_ATTRIBUTES));
    sa.lpSecurityDescriptor = IntPtr.Zero;

    IntPtr h_stdout_r, h_stdout_w;
    if (!CreatePipe(out h_stdout_r, out h_stdout_w, ref sa, 0))
        throw new Exception("bad");
    if (!SetHandleInformation(h_stdout_r, HANDLE_FLAG_INHERIT, 0))
        throw new Exception("bad");

    IntPtr h_stdin_r, h_stdin_w;
    if (!CreatePipe(out h_stdin_r, out h_stdin_w, ref sa, 0))
        throw new Exception("bad");
    if (!SetHandleInformation(h_stdin_w, HANDLE_FLAG_INHERIT, 0))
        throw new Exception("bad");

    si.wShowWindow = 0;
    si.cb = (uint)Marshal.SizeOf(si);
    si.dwFlags |= STARTF_USESTDHANDLES;
    si.hStdOutput = h_stdout_w;
    si.hStdError = h_stdout_w;
    si.hStdInput = h_stdin_r;

    string command = @"C:\flex_sdks\flex_sdk_4.5.1.21328_trimmed\bin\fcsh.exe";

    if (!CreateProcess(command, null, IntPtr.Zero, IntPtr.Zero, true, 0, IntPtr.Zero, null, ref si, out pi))
        throw new Exception("bad");

    Console.WriteLine("Process ID (PID): " + pi.dwProcessId);
    Console.WriteLine("Process Handle : " + pi.hProcess);

    // ****************************************************
    // let's interact with our process

    // first read to the prompt
    Console.WriteLine("read this from fcsh.exe:\r\n" + ReadTillPrompt(h_stdout_r));

    // write "help" to stdin
    byte[] bytes_to_write = Encoding.UTF8.GetBytes("help\r\n");
    Write(h_stdin_w, bytes_to_write, 0, bytes_to_write.Length);

    // then read to the prompt again
    Console.WriteLine("read this from fcsh.exe:\r\n" + ReadTillPrompt(h_stdout_r));

    // write "quit" to stdin
    bytes_to_write = Encoding.UTF8.GetBytes("quit\r\n");
    Write(h_stdin_w, bytes_to_write, 0, bytes_to_write.Length);

    // ****************************************************

    if (!CloseHandle(pi.hProcess))
        throw new Exception("bad");
    if (!CloseHandle(pi.hThread))
        throw new Exception("bad");
    if (!CloseHandle(h_stdout_w))
        throw new Exception("bad");
    if (!CloseHandle(h_stdin_w))
        throw new Exception("bad");
}

public string ReadTillPrompt(IntPtr h_stdout_r)
{
    StringBuilder sb = new StringBuilder(1024);
    byte[] buffer = new byte[128];

    int nb_bytes_read;
    while (true) {
        nb_bytes_read = Read(h_stdout_r, buffer, 0, buffer.Length);
        sb.Append(Encoding.UTF8.GetString(buffer, 0, nb_bytes_read));
        if (sb.ToString().EndsWith("\n(fcsh) "))
            break;
        Thread.Sleep(20);
    }
    return sb.ToString();
}

public unsafe int Read(IntPtr h, byte[] buffer, int index, int count)
{
    int n = 0;
    fixed (byte* p = buffer) {
        if (!ReadFile(h, p + index, count, &n, IntPtr.Zero))
            throw new Exception("bad");
    }
    return n;
}

public unsafe int Write(IntPtr h, byte[] buffer, int index, int count)
{
    int n = 0;
    fixed (byte* p = buffer) {
        if (!WriteFile(h, p + index, count, &n, IntPtr.Zero))
            throw new Exception("bad");
    }
    return n;
}

是的,很抱歉弄糊涂了,我重新回答了我的问题;请以粗体显示“我希望在进程写入时同步读取StdOut”。此外,请注意我的问题是如何询问RedirectStandardInput=true时行为的变化,而不是我如何以不同的方式做事,因为其他人认为我应该这样做。在这种情况下,请尝试我发布的代码,可能对您有用。问候。@sixfeetsix:听起来好像子程序正在使用阻塞读取读取stdin。当stdin未重定向时,它将获得“读取成功,零字节,文件结束”。当stdin被重定向时,阻塞读取永远不会完成。老实说,不要认为在这里选择IPC机制是正确的,至少看一下问题描述。他需要的只是读取和解释输出。IPC意味着进程彼此“对话”,在这种情况下,这将导致需要更改调用进程代码,假设他有权访问该代码。@Tigran,再一次,我对如何以不同方式进行操作的兴趣非常有限。如果您不能解决我的实际问题,请不要在您的意见中添加杂音。@Ben,您最后关于子进程在等待stdin中的某些内容时如何挂起的评论非常有趣;我想我需要更多地了解stdin的工作原理,以及在windows中的工作原理particular@sixfeetsix:这取决于子流程的设计方式。我的工作基于您对观察到的行为的描述,并了解阻塞读取是如何工作的。看起来您真正的问题是缓冲孩子的stdout,再多的stdin重定向也帮不上忙。明白为什么吗?我发布的代码中哪里有问题?如果我禁用重定向stdin,那么读取stdout工作得非常好。你说你不想写一整行,等等。这是一个缓冲问题(与你在这个问题中提到的死锁问题不同)。我不想等待从stdout读取一整行;例如,如果我在cmd中运行同一个.exe,cmd将向我显示一个非终止行,该行将作为提示,例如“(fcsh)”;那么,cmd是如何向我展示这个提示的呢?
cmd
可以随时刷新它的输出缓冲区。大多数程序都不需要刷新,所以它们只需要通过编译器的运行库实现默认行为。大多数库检查stdout是控制台还是文件,控制台输出没有缓冲,文件输出启用缓冲;我上传了一个完整的项目,其中有一个“工作”命令提示符,如果你感兴趣的话:(对不起,默认的命名;))这对我不起作用。它缓冲所有标准输出,直到进程完成。我还没有为.NET找到正确的解决方案。@MarkLakata:上面的代码已经过时,所以我用最新的代码创建了一个博客条目。感受fr