Process.OutputDataReceived 是在哪个线程上引发和处理的?

发布于 2024-10-31 14:36:47 字数 958 浏览 0 评论 0原文

我有一个多线程 winforms 应用程序。一个线程用于 GUI,一个线程用于后台处理。在后台处理中,我通过 Process 类与外部进程通信以发送接收数据。

我对我注册的 Process.OutputDataReceived 处理程序在哪个线程上运行感到困惑。根据 MS 文档:“OutputDataReceived 事件表明关联的进程已写入其重定向的 StandardOutput 流。”但尚不清楚是谁发起了这一事件。

请参见下面的示例代码:

myProc= new Process();
myProc.StartInfo.UseShellExecute = false;
myProc.StartInfo.RedirectStandardOutput = true;
myProc.StartInfo.RedirectStandardError = true;
myProc.StartInfo.RedirectStandardInput = true;
myProc.StartInfo.FileName = "myapp.exe";
myProc.StartInfo.Arguments = arguments;
myProc.StartInfo.CreateNoWindow = true;
myProc.OutputDataReceived += new DataReceivedEventHandler(DataReceivedFromProc);
myProc.ErrorDataReceived += new DataReceivedEventHandler(ErrorReceivedFromProc);
myProc.Start();
myOutputStream = myProc.StandardInput;
myProc.BeginOutputReadLine();
myProc.BeginErrorReadLine();

那么在这种情况下,DataReceivedFromProc 在哪个线程上运行?如果上面的代码在我的 GUI 线程和工作线程上执行,会有什么不同吗?

I have a multi-threaded winforms application. One thread for the GUI, and one thread for background processing. In the background processing, I communicate with an external process via the Process class to send an receive data.

I am confused about what thread the handler that I registered Process.OutputDataReceived is run on. According to MS documentation: "The OutputDataReceived event indicates that the associated Process has written to its redirected StandardOutput stream." But it isn't clear who is raising the event.

See example code below:

myProc= new Process();
myProc.StartInfo.UseShellExecute = false;
myProc.StartInfo.RedirectStandardOutput = true;
myProc.StartInfo.RedirectStandardError = true;
myProc.StartInfo.RedirectStandardInput = true;
myProc.StartInfo.FileName = "myapp.exe";
myProc.StartInfo.Arguments = arguments;
myProc.StartInfo.CreateNoWindow = true;
myProc.OutputDataReceived += new DataReceivedEventHandler(DataReceivedFromProc);
myProc.ErrorDataReceived += new DataReceivedEventHandler(ErrorReceivedFromProc);
myProc.Start();
myOutputStream = myProc.StandardInput;
myProc.BeginOutputReadLine();
myProc.BeginErrorReadLine();

So in this case, what thread is DataReceivedFromProc run on? Does it make a difference if the above is executed on my GUI thread vs worker thread?

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

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

发布评论

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

评论(2

祁梦 2024-11-07 14:36:47

您应该设置 myProc.SynchronizingObject 属性 添加到您的窗体或控件。

否则,我相信该事件将在 IO 完成线程(来自线程池)上引发。

You should set the myProc.SynchronizingObject property to your form or control.

Otherwise, I believe the event will be raised on an IO completion thread (from the ThreadPool).

挖个坑埋了你 2024-11-07 14:36:47

另请参阅 此页面

Process.OutputDataReceived 是在与实例化和配置 Process 对象并启动进程的线程不同的线程上引发的。

如果 Process 对象在主(或 UI)线程上实例化,您将无法从 OutputDataReceived 事件处理程序更新在该线程上运行的 UI。相反,您必须使用委托将消息发送到主线程进行处理。

Also see the user comment at the very bottom of this page:

Process.OutputDataReceived is raised on a different thread than the one that instantiated and configured a Process object, and started the process.

If the Process object is instantiated on the main (or UI) thread, you won't be able to update UI running on that thread from the OutputDataReceived event handler. Instead, you'll have to use delegates to send a message to the main thread for processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文