[英]How can I tell if my code is running async?
This is my first attempt to get code to run async and I can't tell if it actually is.这是我第一次尝试让代码异步运行,但我不知道它是否真的如此。 The function download report does not have an "await" and there is a warning saying it will run synchronously. function 下载报告没有“等待”并且有警告说它将同步运行。
I am attempting to download multiple reports at the same time and then zip them all into one file.我正在尝试同时下载多个报告,然后将 zip 全部下载到一个文件中。
The result is as expected but I would like to be certain that the code is actually performing in async.结果符合预期,但我想确定代码实际上是异步执行的。
static async Task Main(string[] args)
{
string folderName = "Batch123";
string fullDir = Path.Combine(ConfigurationManager.AppSettings["path"], folderName);
Directory.CreateDirectory(fullDir);
await RunReports(folderName);
string zipPath = Path.Combine(ConfigurationManager.AppSettings["path"], "Zip", folderName);
Directory.CreateDirectory(zipPath);
ZipFile.CreateFromDirectory(fullDir, Path.Combine(fullDir, zipPath, "CRAs.zip"));
}
private static async Task RunReports(string folderName)
{
string[] dunsToProcess = new string[] {"d1"
,"d2"
,"d3"
};
await Task.WhenAll(dunsToProcess.Select(i => DownloadReport(i, folderName)));
}
private static async Task DownloadReroport(string DUNS, string folderName)
{
NetworkCredential cred = new NetworkCredential(ConfigurationManager.AppSettings["networkUser"]
, ConfigurationManager.AppSettings["networkPassword"]);
string fullPath = Path.Combine(ConfigurationManager.AppSettings["path"], folderName, string.Format("CRA for DUNS {0}.pdf", DUNS));
WebClient wc = new WebClient();
wc.Credentials = cred;
wc.DownloadFile(@"http://xxxxxxx&pcDUNS=" + DUNS
, fullPath);
}
I hope it is right as it will be the basis of a lot of other changes.我希望它是正确的,因为它将成为许多其他变化的基础。 If not, can you point out what I am doing wrong.如果没有,你能指出我做错了什么。
Feel free to ridicule anything with my code.!!随意用我的代码嘲笑任何东西。!! I have had no c# training at all.我根本没有接受过 c# 培训。
Thank you.谢谢你。
I believe you are referring and threads and tasks and trying to find something async.我相信您指的是线程和任务,并试图找到异步的东西。
As First, I am not sure what you are doing and how this even works when your main method is Task, this make no sense to me, so, try following:首先,我不确定你在做什么以及当你的主要方法是任务时它是如何工作的,这对我来说毫无意义,所以,请尝试以下操作:
ManualResetEvent WaitHandle手动重置事件等待句柄
As short resume, you approach is wrong if I am even able to understand what you are trying to do in this code作为简短的简历,如果我什至能够理解您在此代码中尝试执行的操作,那么您的方法是错误的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.