是异步并专门等待基于GUI的异步编程? [英] Is async and await exclusively for GUI-based asynchronous programming?

查看:176
本文介绍了是异步并专门等待基于GUI的异步编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关新的异步等待在C#运营商,并试图在其中找出情况下,他们将可能对我非常有用。我研究了几个MSDN文章,这里就是我读的字里行间:

I've been reading about the new async and await operators in C# and tried to figure out in which circumstances they would possibly be useful to me. I studied several MSDN articles and here's what I read between the lines:

您可以使用异步 Windows窗体和WPF的事件处理程序,这样他们就可以不用在正在执行的操作的大部分阻塞UI线程中执行冗长的任务。

You can use async for Windows Forms and WPF event handlers, so they can perform lengthy tasks without blocking the UI thread while the bulk of the operation is being executed.

async void button1_Click(object sender, EventArgs e)
{
    // even though this call takes a while, the UI thread will not block
    // while it is executing, therefore allowing further event handlers to
    // be invoked.
    await SomeLengthyOperationAsync();
}

使用方法等待必须异步,这意味着任何使用异步在code某处功能,最终迫使从UI事件处理程序在调用序列中的所有方法,直到最低级的异步方法为异步以及

A method using await must be async, which means that the usage of any async function somewhere in your code ultimately forces all methods in the calling sequence from the UI event handlers up until the lowest-level async method to be async as well.

在换句话说,如果你创建一个普通的好老的ThreadStart 入口点线程(或好老静态INT主控制台应用程序(字串[] args)),那么你就不能使用异步等待因为一个点,你将不得不使用等待,并使用它异步,因此在调用方法的方法您还可以使用等待并作出一个异步等。但是,一旦你到达线程入口点(或的Main()),没有呼叫者其中伺机将产生控制。

In other words, if you create a thread with an ordinary good old ThreadStart entry point (or a Console application with good old static int Main(string[] args)), then you cannot use async and await because at one point you would have to use await, and make the method that uses it async, and hence in the calling method you also have to use await and make that one async and so on. But once you reach the thread entry point (or Main()), there's no caller to which an await would yield control to.

所以基本上你不能使用异步等待,而无需使用标准的WinForms和WPF消息循环的GUI 。我想所有这些都是确实有道理的,因为MSDN指出异步编程并不意味着多线程,但使用UI线程的空闲时间,而不是;使用控制台应用程序或用户定义的入口点线程时,多线程将有必要执行异步操作(如果不使用兼容的消息循环)。

So basically you cannot use async and await without having a GUI that uses the standard WinForms and WPF message loop. I guess all that makes indeed sense, since MSDN states that async programming does not mean multithreading, but using the UI thread's spare time instead; when using a console application or a thread with a user defined entry point, multithreading would be necessary to perform asynchronous operations (if not using a compatible message loop).

我的问题是,这些假设是否准确?

My question is, are these assumptions accurate?

推荐答案

没有,它有很多用途的服务器端的。

No, it has a lot of uses server-side.

我真的建议这个视频:<一href=\"http://channel9.msdn.com/Shows/Going+Deep/Mads-Torgersen-Inside-C-Async\">http://channel9.msdn.com/Shows/Going+Deep/Mads-Torgersen-Inside-C-Async通过的Mads托格森谁是开发C#编译器团队的一部分。我发现它非常有启发。

I really recommend this video: http://channel9.msdn.com/Shows/Going+Deep/Mads-Torgersen-Inside-C-Async by Mads Torgersen who is part of the team that develops the C# compiler. I found it very instructive.

这篇关于是异步并专门等待基于GUI的异步编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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