我如何将 wpf 调度程序转换为 winforms
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18134822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:23:54 来源:igfitidea点击:
how do I convert wpf dispatcher to winforms
提问by ErocM
I was moving over a method to my winforms project from a wpf project.
我正在将一种方法从 wpf 项目转移到我的 winforms 项目。
Everything but this section was moved without issue:
除此部分外的所有内容都没有问题地移动:
private void ServerProcErrorDataReceived(object sender, DataReceivedEventArgs e)
{
// You have to do this through the Dispatcher because this method is called by a different Thread
Dispatcher.Invoke(new Action(() =>
{
richTextBox_Console.Text += e.Data + Environment.NewLine;
richTextBox_Console.SelectionStart = richTextBox_Console.Text.Length;
richTextBox_Console.ScrollToCaret();
ParseServerInput(e.Data);
}));
}
I have no idea how to convert over Dispatcherto winforms.
我不知道如何转换Dispatcher为 winforms。
Can anyone help me out?
谁能帮我吗?
回答by Shimrod
You should use Invoketo replace the Dispatcher.
您应该使用Invoke来替换Dispatcher.
private void ServerProcErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (richTextBox_Console.InvokeRequired)
{
richTextBox_Console.Invoke((MethodInvoker)delegate
{
ServerProcErrorDataReceived(sender, e);
});
}
else
{
richTextBox_Console.Text += e.Data + Environment.NewLine;
richTextBox_Console.SelectionStart = richTextBox_Console.Text.Length;
richTextBox_Console.ScrollToCaret();
ParseServerInput(e.Data);
}
}

