我有一个多线程的winforms应用程序,一个线程负责GUI,另一个线程负责后台处理。在后台处理过程中,我使用Process类与外部进程通信来发送和接收数据。
我困惑于我注册的Process.OutputDataReceived处理程序所运行的线程是哪个。根据微软文档:“OutputDataReceived事件表示关联的Process已写入其重定向的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线程或工作线程上执行,是否会有区别?