简体   繁体   English

如何判断我的代码是否正在异步运行?

[英]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:首先,我不确定你在做什么以及当你的主要方法是任务时它是如何工作的,这对我来说毫无意义,所以,请尝试以下操作:

  1. let main to be as this is default, there is nothing to do in this declaration change让 main 成为默认值,在此声明更改中没有任何事情可做
  2. made class which is doing what you want to do (downloads probably)制作 class 正在做你想做的事(可能下载)
  3. then made Tasks or threads to call many tasks or threads in time.然后使任务或线程及时调用许多任务或线程。 Keep in mind that there is no point to made to many of them, you may want to limit number of tasks/threads executing in time.请记住,对其中许多没有意义,您可能希望限制及时执行的任务/线程的数量。 You may want to have all tasks identified, or you may want just to wait for all of them to be executed, your need and decision, but Task have properites IsCompleted, IsCanceled, etc... and if you are generating threads you may want to investigate following classes and methods:您可能希望识别所有任务,或者您可能只想等待所有任务执行,您的需要和决定,但是 Task 具有属性 IsCompleted、IsCanceled 等......如果您正在生成线程,您可能想要调查以下类和方法:

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.

相关问题 如何告诉我的方法忽略异常并继续运行? - How can I tell my method to ignore an exception and continue running? 如何判断我的程序是否在域控制器上运行? - How can I tell if my program is running on a Domain Controller? 如何判断我的进程是否以管理员身份运行? - How can I tell if my process is running as Administrator? 如何测试以确保此代码确实在异步运行? - How can I test to make sure this code is really running async? 如何判断我的网站是否运行ASP.NET MVC或Web窗体? - How Can I Tell If My Site Is Running ASP.NET MVC or Web Forms? 如何判断我的代码是否已从立即窗口调用? - How can I tell if my code has been called from the Immediate Window? 如何判断我的 IContainerGroup.ExecuteCommandAsync() 代码方法是否运行? - How can I tell if my IContainerGroup.ExecuteCommandAsync() code method ran? 如何才能最好地显示此嵌套集合? (你能告诉我为什么我的代码无效吗?) - How can I best display this nested collection? (Can you tell me why my code isn't working?) 如何判断ValidationSummary是否从后面的代码触发? - How can I tell if ValidationSummary is triggered from code behind? 如何判断或判断一组类是否为“意大利面条代码”? - How can I determine or tell if a set of classes is “spaghetti code”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM