从交流控制台应用程序读取异步标准输出

发布于 2024-09-18 10:37:31 字数 370 浏览 5 评论 0原文

我有一个基于控制台的 c 应用程序。 我使用重定向标准输出从 C# 静默执行它并同步执行,效果很好。 现在我想以异步方式执行此操作,从而提供类似同步方式的输出。 IE OutPutDataRecieved 事件仅在控制台应用程序(exe)完成后才会触发。OutputDataRecieved 事件在完成后为每一行触发,而不是在输出中获得一行时立即触发。

异步代码适用于 CMD.exe 等,因此,我确信其基于 C 的应用程序在输出中存在问题。 仅供参考:c 控制台中的输出是使用 printf 完成的。 根据我的发现: 我认为 c 控制台应用程序在完成执行之前不会向标准输出提供输出/写入。 我尝试将缓冲区设置为 null 或在每次 printf 之后刷新,但没有任何效果。

有什么技巧吗??

I have a console based c app .
I am executing it from c# silently using Redirecting Standard Output and doing it synchronously which works fine.
Now i want to do it in asynch manner which is giving output like synch manner.
i.e
OutPutDataRecieved event is fired but only after the console app(exe) finishes.OutputDataRecieved event is fired for each line after finish, not instantly as soon as it gets a line in output.

The code for asynch works for CMD.exe etc ,So,I am sure its c based app having problem in output.
FYI:The output in c console is done using printf.
Based on my findings:
I think c console app is not giving output/writing to stdout until it finishes its execution.
I tried setting buffer to null or flushing after every printf but none works.

Any tricks??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尤怨 2024-09-25 10:37:31

谢谢,伙计。这很有魅力。

我正在使用 setbuf 将缓冲区设置为空。

真的很感谢你们所有人的努力。

对于其他人的信息,这是我的 C# 代码,也可以在互联网论坛上找到。

        string command = @"Output.exe"; 
  string arguments = "hellotext"; 

  ProcessStartInfo info = new ProcessStartInfo(command, arguments); 

  // Redirect the standard output of the process.  
  info.RedirectStandardOutput = true; 
  info.RedirectStandardError = true; 

  // Set UseShellExecute to false for redirection 
  info.UseShellExecute = false; 

  Process proc = new Process(); 
  proc.StartInfo = info; 
  proc.EnableRaisingEvents = true; 

  // Set our event handler to asynchronously read the sort output. 
  proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived); 
  proc.ErrorDataReceived += new DataReceivedEventHandler(proc_ErrorDataReceived); 
  proc.Exited += new EventHandler(proc_Exited); 

  proc.Start(); 
  // Start the asynchronous read of the sort output stream. Note this line! 
  proc.BeginOutputReadLine(); 
  proc.BeginErrorReadLine(); 

  proc.WaitForExit(); 

  Console.WriteLine("Exited (Main)"); 

} 

static void proc_Exited(object sender, EventArgs e) 
{ 

  Console.WriteLine("Exited (Event)"); 
} 



static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
{ 
  Console.WriteLine("Error: {0}", e.Data); 
} 



static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
  Console.WriteLine("Output data: {0}", e.Data); 
} 

Thanks man.That worked like a charm.

I was using setbuf to set buffer null.

Really appreciate efforts of all you guyz.

FOr info of other guyz,this was my c# code which is available on internet forums and SO too.

        string command = @"Output.exe"; 
  string arguments = "hellotext"; 

  ProcessStartInfo info = new ProcessStartInfo(command, arguments); 

  // Redirect the standard output of the process.  
  info.RedirectStandardOutput = true; 
  info.RedirectStandardError = true; 

  // Set UseShellExecute to false for redirection 
  info.UseShellExecute = false; 

  Process proc = new Process(); 
  proc.StartInfo = info; 
  proc.EnableRaisingEvents = true; 

  // Set our event handler to asynchronously read the sort output. 
  proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived); 
  proc.ErrorDataReceived += new DataReceivedEventHandler(proc_ErrorDataReceived); 
  proc.Exited += new EventHandler(proc_Exited); 

  proc.Start(); 
  // Start the asynchronous read of the sort output stream. Note this line! 
  proc.BeginOutputReadLine(); 
  proc.BeginErrorReadLine(); 

  proc.WaitForExit(); 

  Console.WriteLine("Exited (Main)"); 

} 

static void proc_Exited(object sender, EventArgs e) 
{ 

  Console.WriteLine("Exited (Event)"); 
} 



static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
{ 
  Console.WriteLine("Error: {0}", e.Data); 
} 



static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e) 
{ 
  Console.WriteLine("Output data: {0}", e.Data); 
} 
草莓味的萝莉 2024-09-25 10:37:31

您可以使用 setvbuf 禁用缓冲。

这是一个简单的示例,如果您删除对 setvbuf 的调用,则仅在您按 Enter 键后才会写入重定向内容(等待 getchar())。使用setvbuf,字符串将直接写入重定向流。

int _tmain(int argc, _TCHAR* argv[])
{
  setvbuf(stdout, NULL,_IONBF, 0);
  printf("Hello");
  getchar();
  return 0;
}

You can disable the buffering using setvbuf.

Here is a quick example, if you remove the call to setvbuf then the redirected content is only written once you press enter (waiting on the getchar()). With the setvbuf, the string is written to the redirected stream directly.

int _tmain(int argc, _TCHAR* argv[])
{
  setvbuf(stdout, NULL,_IONBF, 0);
  printf("Hello");
  getchar();
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文