Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的异步编程#_C#_Async Await_Async Ctp - Fatal编程技术网

C# C语言中的异步编程#

C# C语言中的异步编程#,c#,async-await,async-ctp,C#,Async Await,Async Ctp,最近我试图了解新的C#特性,异步编程的async/await关键字。当我在网上搜索时,我遇到了这样一个例子: static void Main(string[] args) { Console.WriteLine("Task based APM demo"); // Call Exponnent() asynchronously. // And immediately return the control flow.

最近我试图了解新的C#特性,异步编程的async/await关键字。当我在网上搜索时,我遇到了这样一个例子:

    static void Main(string[] args)
    {
        Console.WriteLine("Task based APM demo");

        // Call Exponnent() asynchronously.
        // And immediately return the control flow.
        // If I don't put a Task here, the program will sometimes
        // terminate immediately.
        Task t = new Task(async () =>
        {
            int result = await Program.Exponent(10);

            // After the operation is completed, the control flow will go here.
            Console.WriteLine(result);
        });

        t.Start();
        Console.ReadKey();
    }

    static async Task<int> Exponent(int n)
    {
        Console.WriteLine("Task started");
        return await TaskEx.Run<int>(() => 2 << (n - 1));
    }
}
static void Main(字符串[]args)
{
WriteLine(“基于任务的APM演示”);
//异步调用exponnt()。
//并立即返回控制流。
//如果我不把任务放在这里,程序有时会
//立即终止。
任务t=新任务(异步()=>
{
int结果=等待程序指数(10);
//操作完成后,控制流将转到此处。
控制台写入线(结果);
});
t、 Start();
Console.ReadKey();
}
静态异步任务指数(int n)
{
Console.WriteLine(“任务已启动”);
返回等待TaskEx.Run(()=>2
但是为什么这个表达式在指数调用的那一行使用这个呢


我不知道你这个问题是什么意思。
wait Task.Run(()=>2第一个问题是指行:Task t=newtask(async()=>{int result=wait Program.Exponent(10);});如果在方法体中使用了指数方法,为什么我们应该使用等待表达式来调用它?我认为这个问题应该有一个更具体的标题,但我不确定应该是什么。有人有想法吗?@Azad:我建议您从介绍
async
(例如)开始,然后继续介绍。
Task.Run<int>(() => 2 << (n - 1)).ContinueWith(t=>return t.Result);
    static async void Method()
    {
        Console.WriteLine(await Exponent(10));
    }
    static void Main(string[] args)
    {
        Method();
        Console.ReadLine();
    }