如何使用Dispatcher.BeginInvoke正常吗? [英] How do I use Dispatcher.BeginInvoke properly?

查看:128
本文介绍了如何使用Dispatcher.BeginInvoke正常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜查几乎无处不在互联网上,我用Google搜索了这么多次,发现这么多的结果,但我仍然无法找到解决我的问题。

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.

我忙转换旧的的WinForms 应用程序到一个新的WPF应用程序,但我有一些命令的麻烦。在WinForms应用程序,他们使用 Control.BeginInvoke()并存储这种在IAsyncResult对象。我已阅读了 Dispatcher.BeginInvoke() WPF 等同于控制。的BeginInvoke()的WinForms 但是当我使用

I am busy converting an old WinForms application to a new WPF application but I am having trouble with some of the commands. In the Winforms application they use Control.BeginInvoke() and store this in an IAsyncResult object. 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():无法隐式转换类型
'System.Windows.Threading.DispatcherOperation'到'System.IAsyncResult'
的显式转换存在(是否缺少强制?)。

Dispatcher.BeginInvoke(): "Cannot implicitly convert type 'System.Windows.Threading.DispatcherOperation' to 'System.IAsyncResult'. An explicit conversion exists (are you missing a cast?)".

任何帮助将不胜感激。

下面是我试图转换的代码。这是原来的的WinForms 代码。 。我能转换的一切,除了部分的BeginInvoke

Here is the code that I am trying to convert. This is the original WinForms code. I am able to convert everything except the BeginInvoke part.

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



下面是我的代码,我已经转换,到目前为止,包括我正在挣扎的BeginInvoke的一部分。

Below is my code that I have converted so far including the BeginInvoke part that I am struggling with.

    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.

感谢

推荐答案

这是因为 Dispatcher.BeginInvoke ,尽管它可能是等价逻辑操作,不返回的IAsyncResult ,它返回的 的DispatcherOperation 。看看这个博客帖子,你会看到一个关于如何使用调度作品很好的例子。我抄了相关代码示例到这里,以确保其以后的存在。

That's because Dispatcher.BeginInvoke, though it may be the equivalent logical operation, doesn't return an IAsyncResult, it returns a DispatcherOperation. Have a look at this blog post and you'll see a good example on how the Dispatcher works. 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.

这篇关于如何使用Dispatcher.BeginInvoke正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆