Task vs. Task <tresult> </tresult>的不同行为

时间:2014-09-23 14:19:58

标签: c# async-await

在控制台应用程序中,我使用async-await,如下所示:

static void Main(string[] args)
{
    // Task<Model>
    var taskGetModel = Testcases.GetModel(22);
    taskGetModel.Wait();

    // Task without TResult
    var taskSaveModel = Testcases.SaveModel(taskGetModel.Result);
    taskSaveModel.Wait();
}

public async static Task<Model> GetModel(int number)
{
    var baseData = await serviceagent.GetBaseData();

    var model = await serviceagent.GetModel(baseData, number);

    model.BaseData = baseData;

    return model;
}

public static async Task SaveModel(Model model)
{
    await serviceagent.SaveModel(model);
}

// as for the service methods:
public async Task SaveModel(Model model)
{
    // the method `.ToCommunication` has the signature:
    // public async Task<CommunicationModel> ToCommunication
    var commodel = await new Mapper().ToCommunication(model);
    // Proxy is the interface to the servce (ChannelFactory etc.)
    // the method `.Save` has the signature:
    // public async Task Save(CommunicationModel model)
    await Proxy.Save(commodel);
}
public async Task<Model> GetModel(BaseData baseData, int number)
{
   // NOT a task: CommunicationModel GetCommunicationModel
   var commodel = Proxy.GetCommunicationModel(baseData, number);
   // signature with Task: public async Task<Model> ToModel
   return await new Mapper().ToModel(baseData, commodel);
} 

在static main中,Task<TResult>给出了函数GetModel立即返回Task的好结果,我可以等待其结果完成。

为什么SaveModel中的任务不会立即返回? {1}}中的await已在等待该方法。 是因为它没有SaveModel吗?

1 个答案:

答案 0 :(得分:2)

适合我。以下代码......

using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Task<Model>
            var sw = Stopwatch.StartNew();
            Console.WriteLine("1: " + sw.Elapsed);
            var taskGetModel = GetModel(22);
            Console.WriteLine("2: " + sw.Elapsed);
            taskGetModel.Wait();
            Console.WriteLine("3: " + sw.Elapsed);

            // Task without TResult
            var taskSaveModel = SaveModel(taskGetModel.Result);
            Console.WriteLine("4: " + sw.Elapsed);
            taskSaveModel.Wait();
            Console.WriteLine("5: " + sw.Elapsed);
        }

        public async static Task<Model> GetModel(int number)
        {
            var baseData = await service.GetBaseData();

            var model = await service.GetModel(baseData, number);

            model.BaseData = baseData;

            return model;
        }

        public static async Task SaveModel(Model model)
        {
            await service.SaveModel(model);
        }

        static Service service = new Service();
        class Service
        {
            public Task SaveModel(Model model)
            {
                return Task.Delay(1000);
            }

            public async Task<Model> GetModel(object baseData, int number)
            {
                await Task.Delay(1000);
                return new Model();
            }

            public async Task<object> GetBaseData()
            {
                await Task.Delay(1000);
                return new object();
            }
        }
    }
    public class Model
    {
        public object BaseData { get; set; }
    }
}

输出类似

的内容
1: 00:00:00.0000102
2: 00:00:00.0087409
3: 00:00:02.0321182
4: 00:00:02.0356848
5: 00:00:03.0459510

这正是您所期望的。也许你的service.SaveModel方法没有以真正的异步方式实现,并且在放弃控制之前进行长时间运行?这是一个糟糕的实现示例:

public Task SaveModel(Model model)
{
    Thread.Sleep(1000);
    return Task.Delay(0);
}

这使它输出:

1: 00:00:00.0000303
2: 00:00:00.0090621
3: 00:00:02.0345882
4: 00:00:03.0362871
5: 00:00:03.0365073