C# 运行System.Diagnostics进程时如何在线程之间传递对象

C# 运行System.Diagnostics进程时如何在线程之间传递对象,c#,.net,winforms,multithreading,C#,.net,Winforms,Multithreading,我不会提供所有的代码,但会提供一个我想做的示例。我有这段代码用于从外部进程stderr更新GUI元素 我将我的流程设置为: ProcessStartInfo info = new ProcessStartInfo(command, arguments); // Redirect the standard output of the process. info.RedirectStandardOutput = true; info.RedirectStandardError = true; i

我不会提供所有的代码,但会提供一个我想做的示例。我有这段代码用于从外部进程stderr更新GUI元素

我将我的流程设置为:

ProcessStartInfo info = new ProcessStartInfo(command, arguments);

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

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

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();
private void UpdateTextBox(string Text, TextBox Target)
{
    if (this.InvokeRequired)
        this.Invoke(new Action<string, TextBox>(this.SetTextBox), Text, Target);
    else
    {
        Target.AppendText(Text);
        Target.AppendText(Environment.NewLine);
    }
}
然后我有一个事件处理程序

void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        UpdateTextBox(e.Data);
    }
}
它调用以下内容,引用特定的textbox控件

private void UpdateTextBox(string Text)
{
    if (this.InvokeRequired)
        this.Invoke(new Action<string>(this.SetTextBox), Text);
    else
    {
        textBox1.AppendText(Text);
        textBox1.AppendText(Environment.NewLine);
    }
}
private void UpdateTextBox(字符串文本)
{
if(this.invokererequired)
this.Invoke(新操作(this.SetTextBox),Text);
其他的
{
textBox1.AppendText(文本);
textBox1.AppendText(Environment.NewLine);
}
}
我想要的是这样的东西:

ProcessStartInfo info = new ProcessStartInfo(command, arguments);

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

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

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();
private void UpdateTextBox(string Text, TextBox Target)
{
    if (this.InvokeRequired)
        this.Invoke(new Action<string, TextBox>(this.SetTextBox), Text, Target);
    else
    {
        Target.AppendText(Text);
        Target.AppendText(Environment.NewLine);
    }
}
private void UpdateTextBox(字符串文本,TextBox目标)
{
if(this.invokererequired)
this.Invoke(新操作(this.SetTextBox)、文本、目标);
其他的
{
目标文本(Text);
Target.AppendText(Environment.NewLine);
}
}
我可以使用该线程更新不同的文本框,而不必为GUI中的每个控件创建单独的函数

这可能吗?(显然,上述代码无法正常工作)

谢谢

更新

private void UpdateTextBox(string Text, TextBox Target)
{
    if (this.InvokeRequired)
        this.Invoke(new Action<string, TextBox>(this.**UpdateTextBox**), Text, Target);
    else
    {
        Target.AppendText(Text);
        Target.AppendText(Environment.NewLine);
    }
}     
private void UpdateTextBox(字符串文本,TextBox目标)
{
if(this.invokererequired)
this.Invoke(新操作(this.**UpdateTextBox**)、文本、目标);
其他的
{
目标文本(Text);
Target.AppendText(Environment.NewLine);
}
}     

当我注意到一个输入错误时,这段代码现在看起来确实有效。。这可以使用吗?

您提供的代码看起来不错,是在线程之间发送此类消息的好方法。

看看这个

我以前做过这个,但是我现在没有代码


如果我找到了它,我会为您发布它,但这篇文章可能有足够的信息让您了解。

实际上,这看起来是可行的,只是我在复制和粘贴它时没有将SetTextBox更改为UpdateTextBox。您能解释一下您遇到的问题吗?它是可行的,但如果我在那里稍作休息并查看一下IDE中的“Target”表示“'Target.Text'引发了类型为'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessaginException'的异常”