复杂任务的返回类型错误

时间:2019-11-12 21:43:55

标签: c# multithreading async-await task-parallel-library

首先,这篇文章(涉及事件处理程序)不能解决我的问题: Task has a wrong return type

我有以下代码:

public async Task<int> ImportRulesAsync() {
    Task<ConcurrentDictionary<int, SpecialPaintOption>> taskSpecialPaintData = new Task<ConcurrentDictionary<int, SpecialPaintOption>>(populateSpecialPaintOptions);
    taskSpecialPaintData.Start();

    [various processing here...]

    await taskSpecialPaintData;
    [processing that depends on above task to complete]
    return x;
}

private Task<ConcurrentDictionary<int, SpecialPaintOption>> populateSpecialPaintOptions() {
    [sql query here]
    var dtJoin = Common.GetDataTableAsync(sql,  r => new SpecialPaintOption {x = Common.SafeTrim(r["x"]) });
    return dtJoin;
}

dtJoin的类型为Task<ConcurrentDictionary<int, SpecialPaintOption>>GetDataTableAsync具有相同的返回类型:

enter image description here

我收到以下编译错误:

  

populateSpecialPaintOptions()的返回类型错误

编译器在抱怨这一行:

enter image description here

我在做什么错了?

编辑1

当我将签名更改为:

private ConcurrentDictionary<int, SpecialPaintOption> populateSpecialPaintOptions() {...}

...编译器抱怨return dtJoin;

  

无法隐式转换类型   'System.Threading.Tasks.Task>'   'System.Collections.Concurrent.ConcurrentDictionary'

编辑2

我不知道如何在评论中添加屏幕截图,因此这是对Peter提出的使用.Run()的建议的回应,而我的VS2019代码完成情况对此一无所知:

enter image description here

0 个答案:

没有答案