那里。我正在使用 C# .wpf,我从 C# 源代码中获得了一些代码,但我不能使用它。有什么我必须改变的吗?还是做?
// Delegates to enable async calls for setting controls properties
private delegate void SetTextCallback(System.Windows.Controls.TextBox control, string text);
// Thread safe updating of control's text property
private void SetText(System.Windows.Controls.TextBox control, string text)
{
if (control.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
Invoke(d, new object[] { control, text });
}
else
{
control.Text = text;
}
}
如上代码,错误在InvokeRequired
和Invoke
目的是,我有一个内容文本框,将为每个进程增加。
这是文本框的代码。SetText(currentIterationBox.Text = iteration.ToString());
代码有什么问题吗?
感谢您的任何帮助
编辑
// Delegates to enable async calls for setting controls properties
private delegate void SetTextCallback(System.Windows.Controls.TextBox control, string text);
// Thread safe updating of control's text property
private void SetText(System.Windows.Controls.TextBox control, string text)
{
if (Dispatcher.CheckAccess())
{
control.Text = text;
}
else
{
SetTextCallback d = new SetTextCallback(SetText);
Dispatcher.Invoke(d, new object[] { control, text });
}
}