加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么WPF的Dispatcher.Invoke在主线程上运行时不会导致死

发布时间:2020-12-15 23:38:48 所属栏目:百科 来源:网络整理
导读:考虑一下代码: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void button_Click(object sender,RoutedEventArgs e) { //System.Threading.Thread.CurrentThread = button.Dispatcher.Thread button
考虑一下代码:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender,RoutedEventArgs e)
        {
            //System.Threading.Thread.CurrentThread = button.Dispatcher.Thread
            button.Dispatcher.Invoke(() => button.Content = "1234");
        }
    }

当然,button_Click在主线程上运行.

我的理解是button.Dispatcher.Thread是主线程,只有当线程没有被阻塞时才会处理Invoke().但是,在这种情况下,主线程是否被阻止?即主线程正在等待Dispatcher.Invoke()调用完成,Dispatcher.Invoke()正在等待主线程释放.所以我希望在这里遇到僵局,但它不会陷入僵局.

为什么?

P.S:我知道在这种情况下我不需要Dispatcher.Invoke,我可以直接调用button.Content =“1234”.我试图理解为什么在这种情况下不会发生死锁.

解决方法

我相信你的误解可能基于以下思维过程:

“好吧,Invoke会阻塞调用线程,直到操作完成.如果线程被阻塞,它如何在线程上执行操作?”

如果我们查看源代码,我们会看到回调不仅在同一个线程上调用,而且直接在Invoke方法内调用.主线程没有被阻止.

如果查看调度程序的Reference Source页面,您可以在Invoke方法的实现中看到if语句上面的以下注释,并在其中调用回调:

// Fast-Path: if on the same thread,and invoking at Send priority,// and the cancellation token is not already canceled,then just
// call the callback directly.
if(!cancellationToken.IsCancellationRequested && priority == DispatcherPriority.Send && CheckAccess())
{
    /* snipped */

    callback();

    /* snipped */
}

您在主线程上调用Dispatcher.Invoke,该方法只需立即调用它即可处理.

*嗯,不是直接的,但Invoke(Action)的整个主体只是对上面代码所在方法的调用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读