C#类过程 - 控制C 应用程序,不可能读取输出
C# Class Process - Control C++ application, impossibility Read Output
我需要控制C#应用中C 编写的控制台应用程序。C#应用程序无法从C 应用程序的输出中读取任何内容。使用函数process.StandardOutput.ReadLine()
时,应用程序会冻结。我尝试在C#,C 中创建简单的控制台应用,仅使用PRINTF(" Test Shamp r n"(和Console.Writeline("测试示例"(。示例C#应用程序效果很好,C 不可能。所以我不知道我在哪里犯错。可能C 应用程序写了不良的线路,但我不知道该如何解决。
C#中的主应用程序的代码示例,该代码控制了用C 或C#写的另一个应用程序。
process= new Process()
{
StartInfo = new ProcessStartInfo(@"testApp")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = false,
WindowStyle = ProcessWindowStyle.Hidden
}
};
process.Start();
inputWriter = process.StandardInput;
string text =process.StandardOutput.ReadLine();//C++ app freezes here
和两个测试应用
在C
中int main(){
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
printf("Test send datarn");
}
return 0;}
在C#
中static void Main(string[] args)
{
while (true)
{
Thread.Sleep(500);
Console.WriteLine("Test send data");
}
}
看一下以下答案:processstartinfo挂在" waitforeforexit"上。为什么?和standardOutput.readline((使用C#挂起应用程序 他们都可以提供解释和解决方案。
我也尝试使用ReadLineAsync:
while ((line = await process.StandardOutput.ReadLineAsync()) != null)
Console.WriteLine(line);
它起作用。请注意,如果选择了此解决方案,则必须对代码进行一些更改以使其async
(您也可以将其包装在任务中,这完全取决于您的需求(。
相关文章:
- 我想在C++中读取一些多个字符,但它永远不会读取第二个字符
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- C# HashSet VS C++ std::unordered_set 使用自定义类键。C++慢...不可能。如何实现 C# 的速度?
- 为什么在C++不可能递增或进行数学运算,例如在声明为数组的变量上乘法
- fopen 与常量字符 * 从 QString 是不可能的
- 矢量返回常量引用,不可能向下转换
- 分配取消引用的字符指针真的不可能吗?
- boost::asio::async_read_until 不会读取所有消息
- C#类过程 - 控制C 应用程序,不可能读取输出
- 可能是不可能的模板参数规范/推论
- 为什么我的getline不正确读取.csv文件
- C++ fstream 不会读取整个文件
- 读取进程内存 C++ 不会读取
- 易失性结构 = 结构不可能,为什么?
- 矢量超出范围,将值分配给空矢量>不可能?
- 为什么不可能实例化原子对
- 一个倒梯形,但如果输入高度对于宽度来说太大,那么它应该报告,不可能("不可能的形状"是什么)
- W10上不可能使用十字架编译的libpng16-16.dll执行
- 为什么这个文件不成功读取
- 从张量的读取设备不可能的方法