简体   繁体   English

如何正确使用Dispatcher.BeginInvoke?

[英]How do I use Dispatcher.BeginInvoke properly?

I have searched almost everywhere on the internet, and I have googled so many times and found so many results, but I still can't find the solution to my problem. 我几乎到处搜索互联网,我搜索了很多次,发现了很多结果,但我仍然无法找到问题的解决方案。

I am busy converting an old WinForms application to a new WPF application but I am having trouble with some of the commands. 我正忙着将旧的WinForms应用程序转换为新的WPF应用程序,但我遇到了一些命令问题。 In the Winforms application they use Control.BeginInvoke() and store this in an IAsyncResult object. 在Winforms应用程序中,它们使用Control.BeginInvoke()并将其存储在IAsyncResult对象中。 I have read that the Dispatcher.BeginInvoke() is the WPF equivalent to the Control.BeginInvoke() for WinForms but I get this error when I use 我已经读过Dispatcher.BeginInvoke()是与WinFormsControl.BeginInvoke()相当的WPF但我在使用时遇到此错误

Dispatcher.BeginInvoke(): "Cannot implicitly convert type 'System.Windows.Threading.DispatcherOperation' to 'System.IAsyncResult'. An explicit conversion exists (are you missing a cast?)". Dispatcher.BeginInvoke():“无法将类型'System.Windows.Threading.DispatcherOperation'隐式转换为'System.IAsyncResult'。存在显式转换(您是否错过了转换?)”。

Any help will be appreciated. 任何帮助将不胜感激。

Here is the code that I am trying to convert. 这是我试图转换的代码。 This is the original WinForms code. 这是最初的WinForms代码。 I am able to convert everything except the BeginInvoke part. 我能够转换除BeginInvoke部分之外的所有内容。

    private eSkan.api.TeSkanAPI feSkanAPI = null;

    private void MessageFilter_AddRemove_Invoked(bool AddFilter, IMessageFilter Filter)
    {
        if (AddFilter){ Application.AddMessageFilter(Filter); }
        else { Application.RemoveMessageFilter(Filter); }
    }

    private void MessageFilter_AddRemove(bool AddFilter, IMessageFilter Filter)
    {
        {
            IAsyncResult sr = BeginInvoke((ESKAN_ADD_REMOVE_MESSAGEFILTER)MessageFilter_AddRemove_Invoked,
                                          AddFilter, Filter);
            sr.AsyncWaitHandle.WaitOne(2000);
        }
    }

Below is my code that I have converted so far including the BeginInvoke part that I am struggling with. 下面是我到目前为止转换的代码,包括我正在努力的BeginInvoke部分。

    private void MessageFilter_AddRemove_Invoked(bool addFilter, System.Windows.Forms.IMessageFilter filter)
    {
        if (addFilter) 
        { 
            System.Windows.Forms.Application.AddMessageFilter(filter); 
        }
        else 
        {
            System.Windows.Forms.Application.RemoveMessageFilter(filter); 
        }
    }

    private void MessageFilter_AddRemove(bool addFilter, System.Windows.Forms.IMessageFilter filter)
    {
        {
            IAsyncResult sr = System.Windows.Threading.Dispatcher.BeginInvoke((ESKAN_ADD_REMOVE_MESSAGEFILTER)MessageFilter_AddRemove_Invoked, addFilter, filter);
            sr.AsyncWaitHandle.WaitOne(2000);
        }
    }

If there are any other mistakes then please let me know. 如果有任何其他错误,请告诉我。

Thanks 谢谢

That's because Dispatcher.BeginInvoke , though it may be the equivalent logical operation, doesn't return an IAsyncResult , it returns a DispatcherOperation . 这是因为Dispatcher.BeginInvoke虽然可能是等效的逻辑操作,但不返回IAsyncResult ,它返回DispatcherOperation Have a look at this blog post and you'll see a good example on how the Dispatcher works. 看看这篇博客文章 ,您将看到Dispatcher如何工作的一个很好的例子。 I have copied the relevant code example into here to ensure it exists later. 我已将相关代码示例复制到此处以确保它稍后存在。

public Window1()
{
  InitializeComponent();

  CheckBox myCheckBox = new CheckBox();
  myCheckBox.Content = "A Checkbox";

  System.Threading.Thread thread = new System.Threading.Thread(
    new System.Threading.ThreadStart(
      delegate()
      {
        System.Windows.Threading.DispatcherOperation
          dispatcherOp = myCheckBox.Dispatcher.BeginInvoke(
          System.Windows.Threading.DispatcherPriority.Normal,
          new Action(
            delegate()
            {
              myCheckBox.IsChecked = true;
            }
        ));

        dispatcherOp.Completed += new EventHandler(dispatcherOp_Completed);
      }
  ));

  thread.Start();
}

void dispatcherOp_Completed(object sender, EventArgs e)
{
  Console.WriteLine("The checkbox has finished being updated!");
}

Take note to this line: 注意这一行:

dispatcherOp.Completed += new EventHandler(dispatcherOp_Completed);

that's how you're going to know when it's completed - it's going to call back to you via that event. 这就是你知道它何时完成的方式 - 它将通过该事件回电给你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM