等待Task.Factory.StartNew(()=>与Task.Start;等待任务; [英] await Task.Factory.StartNew(() => versus Task.Start; await Task;

查看:665
本文介绍了等待Task.Factory.StartNew(()=>与Task.Start;等待任务;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有这两种形式使用等待着?

Is there any functional difference between these two forms of using await?


string x = await Task.Factory.StartNew(() => GetAnimal("feline"));


  • Task<string> myTask = new Task<string>(() => GetAnimal("feline"));
    myTask.Start();
    string z = await myTask;
    


  • 具体而言,在什么样的顺序在1叫每个操作?被称为StartNew然后就是伺机叫,或伺机在第1首叫什么?

    Specifically, in what order is each operation called in 1.? Is StartNew called and then is await called, or is await called first in 1.?

    推荐答案

    StartNew 只是一个的短手创建和启动任务。如果你想在开始之前做一些事来了工作例如,使用构造函数。如果你只是想立即创建并启动任务,使用短手。

    StartNew is just a short hand for creating and starting a task. If you want to do something to the Task instance before you start it, use the constructor. If you just want to create and start the task immediately, use the short hand.

    的文档 StartNew 说:

    调用StartNew是功能上等同于通过创建任务
      使用它的构造函数之一,然后调用Task.Start方法
      安排任务执行。

    Calling StartNew is functionally equivalent to creating a task by using one of its constructors, and then calling the Task.Start method to schedule the task for execution.

    这篇关于等待Task.Factory.StartNew(()=&GT;与Task.Start;等待任务;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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