使用 Invoke 和 SynchronizationContext.Post 对象有什么区别? [英] What are difference between using Invoke and SynchronizationContext.Post object?

查看:38
本文介绍了使用 Invoke 和 SynchronizationContext.Post 对象有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我收到与线程上下文相关的异常时,我使用委托函数并调用该委托函数.需要使用来自其他线程的控制.但我刚刚了解到我可以使用 SynchronizationContext.Post() 函数,我可以在这个方法中调用我的委托.

When I received an exception that is related with thread context, I use delegate function and I invoke this delegate function. It is necessary for use control from an other thread. But I've just learned that I can use SynchronizationContext.Post() function and I can call my delegate in this method.

我不确定哪个更好?这些方法有什么区别?

I'm not sure which one is better? What are differences between these methods?

推荐答案

是同一个东西,SynchronizationContext.Post()调用BeginInvoke(),Send()调用Invoke().

It is the same thing, SynchronizationContext.Post() calls BeginInvoke() and Send() calls Invoke().

SynchronizationContext 的关键属性是它的实现不止一种.重要的是 WindowsFormsSynchronizationContext,它使用 Control.Begin/Invoke 和 DispatcherSynchronizationContext,它使用 Dispatcher.Begin/Invoke.还有其他适用于 ASP.NET、Windows 应用商店(又名 WinRT,又名通用)应用、COM+ 托管的进程外服务器等.

The key property of SynchronizationContext is that there is more than one implementation of it. Important ones are WindowsFormsSynchronizationContext, it uses Control.Begin/Invoke and DispatcherSynchronizationContext, it uses Dispatcher.Begin/Invoke. There are others for ASP.NET, Windows Store (aka WinRT, aka Universal) apps, out-of-process servers that are COM+ hosted, etcetera.

额外的间接级别有助于避免依赖于调用的特定方法.当然对于任何类库都很重要.

The extra level of indirection helps avoid taking a dependency on the specific method that invokes. Important for any class library of course.

这篇关于使用 Invoke 和 SynchronizationContext.Post 对象有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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