C#.WPF中的线程调用

时间:2011-04-27 11:36:31

标签: c# wpf multithreading invoke invokerequired

那里。 我正在使用C#.wpf,我从C#source获得了一些代码,但我无法使用它。有什么我必须改变的吗?或者呢?

 // 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;
        }
    }

如上面的代码所示,错误位于InvokeRequiredInvoke

目的是,我有一个内容的文本框,将为每个进程递增。

这是文本框的代码。 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 });
        }
    }

3 个答案:

答案 0 :(得分:9)

您可能从Windows窗体中获取了该代码,其中每个Control都有Invoke方法。在WPF中,您需要使用Dispatcher对象,可通过Dispatcher属性访问:

 if (control.Dispatcher.CheckAccess())
 {
     control.Text = text;
 }
 else
 {
     SetTextCallback d = new SetTextCallback(SetText);
     control.Dispatcher.Invoke(d, new object[] { control, text });
 }

此外,您没有正确调用SetText。它需要两个参数,在C#中用逗号分隔,而不是等号:

SetText(currentIterationBox.Text, iteration.ToString());

答案 1 :(得分:6)

在WPF中,你不要使用Control.Invoke,而是使用Dispatcher.Invoke:

Dispatcher.Invoke((Action)delegate(){
  // your code
});

使用

Dispatcher.CheckAccess()

先检查。

答案 2 :(得分:3)

在使用下一个构造的WPF中:

if (control.Dispatcher.CheckAccess())
{
   ...
}
else
{
   control.Dispatcher.Invoke(...)
}
相关问题