您好。我正在使用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 });
}
}