C#'System.InvalidOperationException'不能在进程流上混合同步和异步操作 [英] C# 'System.InvalidOperationException' Cannot mix synchronous and asynchronous operation on process stream
问题描述
Task.Run(()=>
{
while(true)
{
string output = pProcess.StandardOutput.ReadToEnd();
System.Console.Write(output);
}
});
Task.Run
一些异步方法像BeginErrorReadLine或BeginOutputReadLine之前的你的代码。这个SO回答可能有帮助:我试图读取在c#中的进程的输出,但我得到这个消息不能混合同步和异步对过程流进行操作。 I am trying to read from a CMD process repeatedly, and print out the output repeatedly as well, But the while loop I am using is inside a Task.Run so that it doesn't lock the program up, but there is some problem with ASync and non-ASync methods that I don't quite understand. I get that I need to not have the pProcess.StandardOutput.ReadToEnd in a Task.Run, but how would I implement this in my code? Thanks!
Task.Run(() =>
{
while (true)
{
string output = pProcess.StandardOutput.ReadToEnd();
System.Console.Write(output);
}
});
There's no problem with executing this code in a seperate Task with Task.Run
I think you're calling some asynchronous method like BeginErrorReadLine or BeginOutputReadLine somewhere before in your code. This SO answer may help: I am trying to read the output of a process in c# but I get this message "Cannot mix synchronous and asynchronous operation on process stream."
这篇关于C#'System.InvalidOperationException'不能在进程流上混合同步和异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!