简体   繁体   English

当签名为公共异步Task MethodName时,为什么不必返回Task?

[英]Why is it not necessary to return a Task when the signature is public async Task MethodName?

I have some code that is working fine but I don't understand why. 我有一些运行良好的代码,但我不明白为什么。 My method performs some SQL operations using the ExecuteReaderAsync, ExecuteNonQueryAsync, and WriteToServerAsync. 我的方法使用ExecuteReaderAsync,ExecuteNonQueryAsync和WriteToServerAsync执行一些SQL操作。 WriterToServerAsync is the last one I call. WriterToServerAsync是我呼叫的最后一个。

I originally wrote the method with the signature public async void Run() but the Hangfire job runner I am using won't accept async unless it returns a Task. 我最初使用签名公共async void Run()编写了该方法,但是我正在使用的Hangfire作业运行程序不会接受异步,除非它返回Task。 I changed the signature to async Task Run() and then tried to figure out how to return a Task from the method. 我将签名更改为异步Task Run(),然后尝试弄清楚如何从该方法返回Task。

I thought that I could just return the same task that was being returned from WriteToServerAsync but this would not compile. 我以为我可以返回从WriteToServerAsync返回的相同任务,但是无法编译。 I ended deleting the return statement altogether. 我完全删除了return语句。 The compiler does not complain and the method works fine. 编译器不会抱怨,该方法可以正常工作。

public async Task Run()
{
    // Start querying the source first as it is the slowest operation...
    var sqlSourceConnection = new SqlConnection(sourceConnectionString);

    // The query is kind of slow so it needs more than the default 30 second timeout...
    var sqlSourceCommand = new SqlCommand(SourceDataSql, sqlSourceConnection) { CommandTimeout = 180 };

    sqlSourceConnection.Open();

    // Query the records from the source...
    var querySourceDataTask = sqlSourceCommand.ExecuteReaderAsync();

    // Delete existing records from target to make way for the new set...
    var sqlTargetConnection = new SqlConnection(targetConnectionString);

    sqlTargetConnection.Open();
    var sqlTargetTransaction = sqlTargetConnection.BeginTransaction(IsolationLevel.ReadCommitted);

    var deleteRowsTask = sqlDeleteCommand.ExecuteNonQueryAsync();

    // Wait for the delete and query tasks to finish before attempting the bulk copy...
    await Task.WhenAll(deleteRowsTask, querySourceDataTask);

    await sqlBulkCopy.WriteToServerAsync(querySourceDataTask.Result);
}

Why does this compile and work without a return statement for the Task? 为什么在没有Task的返回语句的情况下进行编译和工作?

Why does this compile and work without a return statement for the Task? 为什么在没有Task的返回语句的情况下进行编译和工作?

Because the return type Task is the equivalent of void (a method that doesn't return any value) for an asynchronous method that can be awaited. 因为返回类型Task等于可以等待的异步方法的void(不返回任何值的方法)。

Please refer to the following links for more information about the async/await keywords in C#. 请参考以下链接以获取有关C#中async / await关键字的更多信息。

Async/Await - Best Practices in Asynchronous Programming: https://msdn.microsoft.com/en-us/magazine/jj991977.aspx Async / Await-异步编程最佳实践: https //msdn.microsoft.com/en-us/magazine/jj991977.aspx

Asynchronous Programming with async and await (C#): https://msdn.microsoft.com/en-us/library/mt674882.aspx 具有异步和等待(C#)的异步编程: https : //msdn.microsoft.com/zh-cn/library/mt674882.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM