如何在新的异步/等待在C#5功能与消息循环集成? [英] How does the new async/await feature in C# 5 integrate with the message loop?

查看:303
本文介绍了如何在新的异步/等待在C#5功能与消息循环集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经没有机会检查出CTP新的C#异步电动机/等待功能,但这里的东西我想知道:

I've not had chance to check out the CTP of the new C# async/await feature, but here's something I was wondering:

它是如何与整合消息循环?我认为在一个标准的Windows应用程序(的WinForms,WPF)的延续是通过发送消息到应用程序的消息循环,使用的调度或相似?

How does it integrate with the message loop? I assume that in a standard Windows application (Winforms, WPF) the continuations are called by sending messages to the application's message loop, using a Dispatcher or similar?

如果我不使用标准Windows消息循环?例如,在GTK#应用程序或一个控制台应用程序(如果确实此功能可以使用在所有的控制台应用程序)。

What if I'm not using a standard windows message loop? For example in a GTK# application or in a console application (if indeed this feature could be of use at all in a console application).

我在网上搜索有关此,但无济于事信息。任何人都可以解释一下吗?

I've searched the internet for information about this but to no avail. Can anyone explain?

推荐答案

它使用System.Threading.SynchronizationContext.Current。 WPF和安装的WinForms自己的SynchronizationContext的版本。它用自己的消息循环元帅从一个工作线程调用返回到主UI线程。分别与Dispatcher.Begin / Invoke并Control.Begin / Invoke的()。

It uses System.Threading.SynchronizationContext.Current. Both WPF and Winforms install their own version of SynchronizationContext. Which use their message loop to marshal the call from a worker thread back to the main UI thread. Respectively with Dispatcher.Begin/Invoke and Control.Begin/Invoke().

入门本在控制台模式的应用程序做是不容易的,它的主线程没有一个明确的空闲状态,允许喷入封在避免重入头痛安全的方式方法调用。你当然可以添加,但你会被重新发明了消息循环这样做。

Getting this done in a Console mode app is not easy, its main thread doesn't have a well defined 'idle' state that would allow injecting marshaled method calls in a safe manner that avoids re-entrancy headaches. You could certainly add it but you'll be re-inventing the message loop doing so.

这篇关于如何在新的异步/等待在C#5功能与消息循环集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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