【发布时间】:2015-01-12 19:37:16
【问题描述】:
我使用 Main 中的 .Wait() 调用 system.Threading.Tasks.Task 方法。该方法最后有一个 return 语句,我希望它表示任务已经“完成”,允许 Main 继续执行。但是,返回语句被命中(使用断点调试),但在 Main 中执行不会继续,而是似乎只是“挂起”,没有进一步执行。
代码在这里。
using System;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Dfareporting.v1_3;
using Google.Apis.Dfareporting.v1_3.Data;
using _file = Google.Apis.Dfareporting.v1_3.Data.File;
using Google.Apis.Download;
using Google.Apis.Services;
using Google.Apis.Util.Store;
namespace DCMReportRetriever
{
public class DCMReportRetriever
{
public static void Main()
{
new DCMReportRetriever().Run().Wait();
return; // This statement is never executed
}
private async System.Threading.Tasks.Task Run()
{
...
foreach (_file f in files)
{
if (f.Status == "REPORT_AVAILABLE" && f.LastModifiedTime >= startDateSinceEpoch)
{
using (var stream = new FileStream(f.FileName + ".csv", FileMode.Append))
{
new MediaDownloader(service).Download(f.Urls.ApiUrl, stream);
}
}
}
return; // This statement is hit (debugged with breakpoint)
}
}
}
编辑:我应该补充说 Main 是我的入口点,显然 static async void Main 不好?
【问题讨论】:
-
请显示调用
Run的部分代码。 -
首先,return 语句是多余的。第二:发布一个简短但完整的程序,可以重现问题。参考MVCE
标签: c# .net asynchronous async-await