小编iwe*_*daz的帖子

创建异步包装器的方法

如何为同步方法创建异步包装器更好?

// sync method
public void LongOperation()
{
    //code...
}


// versions of wrapper
public async Task LongOpertionWrapperAsyncV1()
{
    var task = Task.Factory.StartNew(LongOperation);
    await task.ConfigureAwait(false);
}

public Task LongOpertionWrapperAsyncV2()
{
    var task = Task.Factory.StartNew(LongOperation);
    task.ConfigureAwait(false);
    return task;
}
Run Code Online (Sandbox Code Playgroud)

虽然两个版本的使用没有区别.

async Task Executor()
{
    await LongOpertionWrapperAsyncV1();
    await LongOpertionWrapperAsyncV2();
}
Run Code Online (Sandbox Code Playgroud)

对于返回值的方法(Task <T>),我使用第一个版本.

但我想知道你的意见.

这些版本之间存在一般差异吗?

.net c# async-await c#-5.0

6
推荐指数
2
解决办法
6812
查看次数

标签 统计

.net ×1

async-await ×1

c# ×1

c#-5.0 ×1