Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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# process.OutputDataReceived未读取所有行_C# - Fatal编程技术网

C# process.OutputDataReceived未读取所有行

C# process.OutputDataReceived未读取所有行,c#,C#,来自cmd的结果 C:\Users\XXXXX>adb start-server * daemon not running. starting it now * * daemon started successfully * C:\Users\XXXXX> 我的c代码 现在,当我调用devicesPlus时,有时我只得到*守护进程没有运行。现在就开始* 有时它只是在后台工作,没有结果。。 你们能告诉我我的代码出了什么问题吗,为什么我不能得到像cmd一样的正确返回。。 刚到c#抱歉

来自cmd的结果

C:\Users\XXXXX>adb start-server
* daemon not running. starting it now *
* daemon started successfully *

C:\Users\XXXXX>
我的c代码

现在,当我调用devicesPlus时,有时我只得到*守护进程没有运行。现在就开始* 有时它只是在后台工作,没有结果。。 你们能告诉我我的代码出了什么问题吗,为什么我不能得到像cmd一样的正确返回。。 刚到c#抱歉英语不好

更新
如果我在我的应用程序外杀死adb,我会突然收到软件的回复。

添加以下行,以阅读:

WaitForExit()
只等待进程退出。它不会等待您的进程接收所有输出,因此您在那里有一个竞争条件

call\u OutputDataReceived
将使用
e.Data==null调用,以表示输出结束。在使用
outData.ToString()
之前,需要等待该调用

例如,您可以使用
新倒计时事件(2)
等待两个流的结束:

    CountdownEvent countdownEvent;

    public string devicesPlus()
    {
        psi.Arguments = "start-server";
        countdownEvent = new CountdownEvent(2);
        call = Process.Start(psi);
        call.OutputDataReceived += new DataReceivedEventHandler(call_OutputDataReceived);
        call.ErrorDataReceived += new DataReceivedEventHandler(call_OutputDataReceived);
        call.EnableRaisingEvents = true;
        call.Exited += new EventHandler(call_Exited);
        call.Start();
        call.BeginOutputReadLine();
        call.BeginErrorReadLine();
        call.StandardInput.Close();
        call.WaitForExit();
        countdownEvent.Wait();
        return outData.ToString();
    }

    private void call_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null)
        {
            // prevent race condition when data is received form stdout and stderr at the same time
            lock (outData)
            {
                outData.Append(e.Data);
            }
        }
        else
        {
            // end of stream
            countdownEvent.AddCount();
        }
    }

同样的先生,这一切都没有发生..我在ProcessStartInfo上添加了一些设置,删除了额外的启动调用。。尝试一下…我更新了post plz帮助,先生..代码无法按post方式工作,UseShellExecute属性分配丢失。不使用CreateNoWindow和不重定向输入是有风险的。显然,如果你不发布准确的代码,你就无法得到准确的答案。我知道,先生,psi是抽象类的一部分。亲爱的先生,谢谢你的回复,我尝试了,但什么都没有发生,如果我从我的c#app外杀死adb,我会突然得到回复,但我无法修复它。。
psi.Arguments = "start-server";
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;

call = Process.Start(psi);
call.OutputDataReceived += new DataReceivedEventHandler(call_OutputDataReceived);
call.ErrorDataReceived += new DataReceivedEventHandler(call_OutputDataReceived);
call.Exited += new EventHandler(call_Exited);

call.EnableRaisingEvents = true;

call.BeginOutputReadLine();
call.BeginErrorReadLine();
call.StandardInput.Close();

call.WaitForExit();

return outData.ToString();
    CountdownEvent countdownEvent;

    public string devicesPlus()
    {
        psi.Arguments = "start-server";
        countdownEvent = new CountdownEvent(2);
        call = Process.Start(psi);
        call.OutputDataReceived += new DataReceivedEventHandler(call_OutputDataReceived);
        call.ErrorDataReceived += new DataReceivedEventHandler(call_OutputDataReceived);
        call.EnableRaisingEvents = true;
        call.Exited += new EventHandler(call_Exited);
        call.Start();
        call.BeginOutputReadLine();
        call.BeginErrorReadLine();
        call.StandardInput.Close();
        call.WaitForExit();
        countdownEvent.Wait();
        return outData.ToString();
    }

    private void call_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null)
        {
            // prevent race condition when data is received form stdout and stderr at the same time
            lock (outData)
            {
                outData.Append(e.Data);
            }
        }
        else
        {
            // end of stream
            countdownEvent.AddCount();
        }
    }