WPF Dispatcher 的 InvokeAsync 和 BeginInvoke 有什么区别

编程入门 行业动态 更新时间:2024-10-26 11:14:11
本文介绍了WPF Dispatcher 的 InvokeAsync 和 BeginInvoke 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我注意到在 .NET 4.5 中 WPF Dispatcher一组新的方法,用于在 Dispatcher 的线程上执行名为 InvokeAsync 的内容.在 .NET 4.5 之前,我们有 调用 和 BeginInvoke 分别同步和异步处理.

I noticed in .NET 4.5 that the WPF Dispatcher had gotten a new set of methods to execute stuff on the Dispatcher's thread called InvokeAsync. Before, .NET 4.5 we had Invoke and BeginInvoke which handled this syncronously and asynchronously respectively.

除了命名和可用的重载略有不同之外,BeginInvoke 和 InvokeAsync 方法之间有什么主要区别吗?

Besides the naming and the slightly different overloads available, are there any major differences between the BeginInvoke and the InvokeAsync methods?

哦,我已经检查过了,两者都可以awaited:

Oh, and I already checked, both can be awaited:

private async Task RunStuffOnUiThread(Action action) { // both of these works fine await dispatcher.BeginInvoke(action); await dispatcher.InvokeAsync(action); }

推荐答案

没有区别,因为 BeginInvoke 方法调用私有的 LegacyBeginInvokeImpl 方法,而它的lef 调用私有方法InvokeAsyncImpl(InvokeAsync 使用的方法).所以它基本上是一样的.看起来这是一个简单的重构,但奇怪的是 BeginInvoke 方法没有被标记为过时.

There are no differences as the BeginInvoke method calls a private LegacyBeginInvokeImpl method which itslef calls the private method InvokeAsyncImpl (the method used by InvokeAsync). So it's basically the same thing. It seems like it's a simple refactoring, however it's strange the BeginInvoke methods weren't flagged as obsolete.

开始调用:

public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method) { return this.LegacyBeginInvokeImpl(priority, method, null, 0); } private DispatcherOperation LegacyBeginInvokeImpl(DispatcherPriority priority, Delegate method, object args, int numArgs) { Dispatcher.ValidatePriority(priority, "priority"); if (method == null) { throw new ArgumentNullException("method"); } DispatcherOperation dispatcherOperation = new DispatcherOperation(this, method, priority, args, numArgs); this.InvokeAsyncImpl(dispatcherOperation, CancellationToken.None); return dispatcherOperation; }

调用异步:

public DispatcherOperation InvokeAsync(Action callback, DispatcherPriority priority) { return this.InvokeAsync(callback, priority, CancellationToken.None); } public DispatcherOperation InvokeAsync(Action callback, DispatcherPriority priority, CancellationToken cancellationToken) { if (callback == null) { throw new ArgumentNullException("callback"); } Dispatcher.ValidatePriority(priority, "priority"); DispatcherOperation dispatcherOperation = new DispatcherOperation(this, priority, callback); this.InvokeAsyncImpl(dispatcherOperation, cancellationToken); return dispatcherOperation; }

更多推荐

WPF Dispatcher 的 InvokeAsync 和 BeginInvoke 有什么区别

本文发布于:2023-11-25 16:32:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630506.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   Dispatcher   WPF   BeginInvoke   InvokeAsync

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!