异步等待最佳实践 [英] async await best practices

查看:137
本文介绍了异步等待最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经掌握了概念异步的等待,并一直在使用它断断续续,但确实有关于最佳实践对夫妇的问题。

  1. 是确定使用伺机在一段时间(条件)循环,以保持获取的数据,可能是present,直到当病情变化,如: stopProcessingMessages =假的。

  2. 在如的WinForms应用程序,而用户界面运行在它的线程,使用异步/等待的操作,如点击一个按钮是相当微不足道的,但是,如果我想异步执行贯穿整个控制台应用程序,甚至是窗口服务。什么是最初揭开序幕,首先计谋任务的最佳实践,那又是Task.Run(()=> ...)?

我希望我做感在我的第二个问题。我想充分利用异步,并利用它来它的充分程度,只是需要了解如何揭开序幕初始异步操作之前,泡沫到所有其他异步功能。

道歉没有使用正确的code块,我在火车上用我的智能手机。

解决方案
  

我已经掌握了概念异步的等待,并一直在使用它断断续续,但确实有关于最佳实践对夫妇的问题。

我有一个介绍到异步 / 等待博客文章的进入比大多数前奏更详细信息,并介绍了几种最佳实践。

  

是确定使用在等待一段时间(条件)循环,以保持获取的数据,可能是present,直到当病情变化,如: stopProcessingMessages =假的。

您想避免紧密循环。因此,而(条件)GetDataIf present(); 将消耗大量的CPU

另外,你可以使用异步的返回方法(或其他),如果 stopProcessingMessages 。在这种情况下,你的code将,而(真),更TAP样的解决办法是使用 CancellationSource ,而不是一个标志。

另外一起来看看 TPL数据流的;这听起来像它可能是您的一种情况是有用的。

  

控制台应用程序,甚至是窗口服务。什么是先开球的最佳做法,首先计谋任务

有关控制台应用程序,你可以等待在顶级的任务。这是一个可以接受的例外平时指引(这是等待,而不是的等待)。 等待 ING将燃烧线程的控制台应用程序的时间,但是这通常不是重要的,足以保证一个更复杂的解决方案。如果你想安装一个单独的线程上下文控制台应用程序,您可以使用 < $ C C> AsyncContext.Run $从我的 AsyncEx库

对于Win32服务,你通常<一href="http://blogs.msdn.com/b/bclteam/archive/2009/02/19/in-depth-how-net-managed-services-interact-with-the-servicecontrolmanager-scm-kim-hamilton.aspx"相对=nofollow>就需要开始自己的线程。您可以使用 Task.Run 这个(如果你想有一个多线程的上下文中),或 AsyncContextThread 从AsyncEx(如果你想要一个单线程上下文)。

I've grasped the concept of async await and have been using it sporadically, but do have a couple questions regarding best practices.

  1. is it ok to use await in a while(condition) loop to keep fetching data that may be present, until the while condition changes, e.g. stopProcessingMessages = false.

  2. in an application such as winforms, while UI runs on it's thread, using async/await on an operation such as a button click is fairly trivial, but what about if I would like to enforce asynchronously throughout an entire console application, or even a windows service. what is the best practice to initially kick off that first await task, would that be Task.Run (() => ... )?

I hope I am making sense in my 2nd question. I want to make the most of async and utilize it to it's full extent, but just need to understand how to kick off the initial asynchronous operation before it bubbles down to all other asynchronous functions.

apologies for not using the proper code blocks I am on the train using my smartphone.

解决方案

I've grasped the concept of async await and have been using it sporadically, but do have a couple questions regarding best practices.

I have an intro to async/await blog post that goes into more detail than most intros and also introduces several best practices.

is it ok to use await in a while(condition) loop to keep fetching data that may be present, until the while condition changes, e.g. stopProcessingMessages = false.

You want to avoid tight loops. So the while (condition) GetDataIfPresent(); is going to consume a lot of CPU.

Alternatively, you could use an async method that returned null (or whatever) if stopProcessingMessages is true. In this case, your code would be while (true), and a more TAP-like solution would be to use CancellationSource instead of a flag.

Also take a look at TPL Dataflow; it sounds like it may be useful for your kind of situation.

console application, or even a windows service. what is the best practice to initially kick off that first await task

For console apps, you could Wait on the top-level task. This is an acceptable exception to the usual guideline (which is to await instead of Wait). Waiting will burn a thread for the duration of the console app, but that's usually not important enough to warrant a more complex solution. If you do want to install a single-threaded context for your console app, you could use AsyncContext.Run from my AsyncEx library.

For Win32 services, you usually do need to start your own thread. You can use Task.Run for this (if you want a multithreaded context), or AsyncContextThread from AsyncEx (if you want a single-threaded context).

这篇关于异步等待最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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