异步于控制台应用程序的主要方法 [英] Async on main method of console app

查看:211
本文介绍了异步于控制台应用程序的主要方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新手在异步世界,我试图找出如何确保我的主要的控制台应用程序,方法,实际运行的异步

I am fairly novice in the async world, and I am trying to figure out how to make sure my main method of an console app actually runs async

class Program
{
    static void Main(string[] args)
    {
        Bootstrapper bs = new Bootstrapper();
        var list = bs.GetList();
    }
}

public class Bootstrapper {

    public async Task<List<TvChannel>> GetList()
    {
        GetPrograms pro = new GetPrograms();

        return await pro.DownloadTvChannels();
    }
}

我知道这是不是从顶运行异步。但因为你不能标注主要用异步我不知道如何来确保。有任何想法吗 ?谢谢!

I know this is not running async from "the top". but since you cannot mark the Main with async I have no idea of how to make sure. Any ideas ? Thank you!

推荐答案

当你发现,在VS11编译器将不允许一个异步主方法。这是允许的(但绝不推荐)在VS2010与异步CTP。

As you discovered, in VS11 the compiler will disallow an async Main method. This was allowed (but never recommended) in VS2010 with the Async CTP.

我有关于异步/计谋和的asynchronous控制台程序的特别。下面是从后介绍一些背景信息:

I have recent blog posts about async/await and asynchronous console programs in particular. Here's some background info from the intro post:

如果等待看到该awaitable还没有完成,那么它的行为是异步的。它告诉awaitable运行该方法的其余部分时它完成,然后从异步方法然后返回的。等待也将捕捉当前的上下文当它通过该方法的awaitable的剩余部分。

If "await" sees that the awaitable has not completed, then it acts asynchronously. It tells the awaitable to run the remainder of the method when it completes, and then returns from the async method. Await will also capture the current context when it passes the remainder of the method to the awaitable.

稍后,awaitable完成时,它将执行所述异步方法的其余部分(所捕获范围内)。

Later on, when the awaitable completes, it will execute the remainder of the async method (within the captured context).

这也是为什么这是与控制台程序异步主一个问题:

Here's why this is a problem in Console programs with an async Main:

这是我们的介绍后请记住,一个异步方法的返回的到它的调用者在完成之前。这工作完全在UI应用程序(该方法仅返回到UI事件循环)和ASP.NET应用程序(该方法返回关闭线程,但保持请求活着)。它不工作这么好,给控制台程序:主返回操作系统 - 让你的程序退出

Remember from our intro post that an async method will return to its caller before it is complete. This works perfectly in UI applications (the method just returns to the UI event loop) and ASP.NET applications (the method returns off the thread but keeps the request alive). It doesn't work out so well for Console programs: Main returns to the OS - so your program exits.

一种解决方案是提供自己的背景 - 一个主循环为您的控制台程序,它是异步兼容

One solution is to provide your own context - a "main loop" for your console program that is async-compatible.

如果你有一台机器与异步CTP,您可以使用 GeneralThreadAffineContext 我的文档\微软的Visual Studio异步CTP \样品(C#测试)单元测试\ AsyncTestUtilities 的。另外,您也可以使用 AsyncContext 从< A HREF =htt​​p://nuget.org/packages/Nito.AsyncEx>我Nito.AsyncEx的NuGet包。

If you have a machine with the Async CTP, you can use GeneralThreadAffineContext from My Documents\Microsoft Visual Studio Async CTP\Samples(C# Testing) Unit Testing\AsyncTestUtilities. Alternatively, you can use AsyncContext from my Nito.AsyncEx NuGet package.

下面是使用的例子 AsyncContext ; GeneralThreadAffineContext 具有几乎相同的用法:

Here's an example using AsyncContext; GeneralThreadAffineContext has almost identical usage:

using Nito.AsyncEx;
class Program
{
    static void Main(string[] args)
    {
        AsyncContext.Run(() => MainAsync(args));
    }

    static async void MainAsync(string[] args)
    {
        Bootstrapper bs = new Bootstrapper();
        var list = await bs.GetList();
    }
}

这篇关于异步于控制台应用程序的主要方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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