相关疑难解决方法(0)

如何以及何时使用'async'和'await'

从我的理解主要事情之一asyncawait要做的就是让代码易于读写-但使用它们等于产卵后台线程来执行持续时间长的逻辑?

我正在尝试最基本的例子.我在内联添加了一些评论.你能为我澄清一下吗?

// I don't understand why this method must be marked as `async`.
private async void button1_Click(object sender, EventArgs e)
{
    Task<int> access = DoSomethingAsync();
    // task independent stuff here

    // this line is reached after the 5 seconds sleep from 
    // DoSomethingAsync() method. Shouldn't it be reached immediately? 
    int a = 1; 

    // from my understanding the waiting should be done here.
    int x = await access; 
}

async Task<int> DoSomethingAsync()
{
    // is …
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous async-await

989
推荐指数
19
解决办法
78万
查看次数

在C#中"返回等待"的目的是什么?

没有像这样编写方法的场景:

public async Task<SomeResult> DoSomethingAsync()
{
    // Some synchronous code might or might not be here... //
    return await DoAnotherThingAsync();
}
Run Code Online (Sandbox Code Playgroud)

而不是这个:

public Task<SomeResult> DoSomethingAsync()
{
    // Some synchronous code might or might not be here... //
    return DoAnotherThingAsync();
}
Run Code Online (Sandbox Code Playgroud)

会有意义吗?

为什么return await在可以直接Task<T>从内部DoAnotherThingAsync()调用返回时使用构造?

return await在很多地方看到代码,我想我应该错过一些东西.但据我了解,在这种情况下不使用async/await关键字并直接返回Task将在功能上等效.为什么要增加附加await层的额外开销?

.net c# async-await .net-4.5

219
推荐指数
7
解决办法
3万
查看次数

在方法名称中使用"Async"后缀是否取决于是否使用了"async"修饰符?

使用"Async"后缀方法名称的约定是什么?

"Async"后缀是否应附加到使用async修饰符声明的方法?

public async Task<bool> ConnectAsync()
Run Code Online (Sandbox Code Playgroud)

或者该方法刚刚返回Task<T>还是足够Task

public Task<bool> ConnectAsync()
Run Code Online (Sandbox Code Playgroud)

.net c# naming naming-conventions async-await

90
推荐指数
7
解决办法
3万
查看次数

在异步方法结束时,我应该返回还是等待?

在任务返回异步方法结束时,如果我调用另一个异步方法,我可以使用await它或return它的任务.每个的后果是什么?

    Task FooAsync()
    {
        return BazAsync();  // Option A
    }

    async Task BarAsync()
    {
        await BazAsync(); // Option B
    }
Run Code Online (Sandbox Code Playgroud)

c# async-await

35
推荐指数
1
解决办法
4538
查看次数

我应该将任务包装在另一个任务中,还是应该只返回创建的任务?

我正在构建一个使用ADO.NET的.NET 4.0应用程序,所以我不能使用async/await.我不想要一个解决方案,但我想知道以下哪些实现最好,为什么.我的单元测试通过所有三个实现,但我想知道这三个之间的区别.

#1嵌套任务

在我的第一个实现中,我将任务包装在另一个任务中.我认为搞两个任务对性能不利,但我不确定.

public virtual Task<IDataReader> ExecuteReaderAsync(IDbCommand dbCommand, CancellationToken cancellationToken)
{
    return Task.Factory.StartNew(() =>
    {
        var sqlCommand = CheckIfSqlCommand(dbCommand);
        PrepareExecuteReader(dbCommand);

        return Task<IDataReader>
            .Factory
            .FromAsync(sqlCommand.BeginExecuteReader, sqlCommand.EndExecuteReader, null)
            .Result;
    }, cancellationToken);
}
Run Code Online (Sandbox Code Playgroud)

#2使用TaskCompletionSource

然后我尝试将结果包装成一个TaskCompletionSource所以我只有一个任务.

public virtual Task<IDataReader> ExecuteReaderAsync(IDbCommand dbCommand, CancellationToken cancellationToken)
{
    var taskCompletionSource = new TaskCompletionSource<IDataReader>();
    var sqlCommand = CheckIfSqlCommand(dbCommand);
    PrepareExecuteReader(dbCommand);

    var reader = Task<IDataReader>
        .Factory
        .FromAsync(sqlCommand.BeginExecuteReader, sqlCommand.EndExecuteReader, null)
        .Result;

    taskCompletionSource.SetResult(reader);

    return taskCompletionSource.Task;
}
Run Code Online (Sandbox Code Playgroud)

#3直接返回任务

我的最终解决方案是直接返回我创建的任务而不是包装它.

public virtual Task<IDataReader> ExecuteReaderAsync(IDbCommand dbCommand, CancellationToken cancellationToken)
{
    var sqlCommand = CheckIfSqlCommand(dbCommand); …
Run Code Online (Sandbox Code Playgroud)

.net c# ado.net .net-4.0 task-parallel-library

5
推荐指数
1
解决办法
1031
查看次数