不能指定一个控制台应用程序的“主”方法“异步修饰符 [英] Can't specify the 'async' modifier on the 'Main' method of a console app

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

问题描述

我是新来的异步修改异步编程。我试图找出如何确保我的控制台应用程序的方法其实异步运行。

I am new to asynchronous programming with the async modifier. I am trying to figure out how to make sure that my Main method of a console application actually runs asynchronously.

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();
    }
}

我知道这是不是异步运行的顶。因为它是无法指定的方法异步修改,我怎么可以运行在code 异步?

I know this is not running asynchronously from "the top." Since it is not possible to specify the async modifier on the Main method, how can I run code within main asynchronously?

推荐答案

当你发现,在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.

我对异步/的await 并的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事件循环)和ASP.NET应用程序(该方法返回关闭线程,但保持请求活着)。它并不适用于控制台程序一起工作这么好:主返回OS - 让你的程序退出

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天全站免登陆