复杂任务的返回类型错误

首先,这篇文章(涉及事件处理程序)不能解决我的问题: 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具有相同的返回类型:

复杂任务的返回类型错误

我收到以下编译错误:

  

populateSpecialPaintOptions()的返回类型错误

编译器在抱怨这一行:

复杂任务的返回类型错误

我在做什么错了?

编辑1

当我将签名更改为:

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

...编译器抱怨return dtJoin;

  

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

编辑2

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

复杂任务的返回类型错误

deitysix 回答:复杂任务的返回类型错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3114100.html

大家都在问