![](/img/trans.png)
[英]Can I replace a call to await Task.Delay with an async method that has inside a await Task.Delay?
[英]Async Await and how to avoid await Task.Delay
我正在嘗試從一個網址檢索100個頁面。 網址的格式如下: https : //www.someBlogSite.com/thread.php?t=xxxx& page =
因此,我的for循環基本上遍歷這些頁面並將結果存儲在本地磁盤上的html文件中。
https://www.someBlogSite.com/thread.php?t=xxxx&page=1 https://www.someBlogSite.com/thread.php?t=xxxx&page=2 。 。 https://www.someBlogSite.com/thread.php?t=xxxx&page=99
這是我的工作代碼, 有沒有一種方法可以避免使用等待Task.Delay(10000)? 之所以必須使用它,是因為否則我的代碼將在檢索所有100頁內容之前退出。
using System;
using System.Linq;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
namespace RetrieveImages
{
public class PerformingGet
{
static void Main(string[] args) => MainAsync(args).Wait();
static async Task MainAsync(string[] args)
{
//Instantiating HttpClient once in the main method since this is costly
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Base Url--going forward only this will need to be edited per collection
string pageBaseUrl = "https://www.someBlogSite.com/thread.php?t=xxxx&page=";
var collectionOfUrls= new List<string>();
for (int pageNum = 1; pageNum < 100; pageNum++)
{
string pageUrl = pageBaseUrl + pageNum;
collectionOfUrls.Add(pageUrl);
}
var tasks = collectionOfUrls.Select(url => GetRequestString(client, url));
var results = await Task.WhenAll(tasks);
var text = string.Join("\r\n", results);
WriteTextAsync(text);
foreach (var x in collectionOfUrls)
{
Console.WriteLine(x);
//await GetRequest(client, x);
}
Console.WriteLine("Task completed");
await Task.Delay(10000);
}
async static Task<string> GetRequestString(HttpClient client, string Url)
{
using (HttpResponseMessage response = await client.GetAsync(Url))
{
if (response.IsSuccessStatusCode)
{
using (HttpContent content = response.Content)
{
return await content.ReadAsStringAsync();
}
}
return string.Empty;
}
}
static async void WriteTextAsync(string text)
{
// Set a variable to the My Documents path.
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Write the text asynchronously to a new file named "WriteTextAsync.txt".
using (StreamWriter outputFile = new StreamWriter(Path.Combine(mydocpath, "WriteTextAsync.html")))
{
await outputFile.WriteAsync(text);
}
}
}
}
永遠不要使用異步void(除非您真的知道自己在做什么,或者Visual Studio為您生成了異步異步,例如事件處理程序)。 如果您不需要從async
方法返回任何內容,請讓它返回一個Task
(根本沒有泛型類型參數)。 您的代碼實際上不必返回它; 編譯器會為您執行此操作。
static async Task WriteTextAsync(string text)
{
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using (StreamWriter outputFile = new StreamWriter(Path.Combine(mydocpath, "WriteTextAsync.html")))
{
await outputFile.WriteAsync(text);
}
//Notice no return statement
}
即使它沒有返回值,這也將允許您等待它。
await WriteTextAsync(text);
如果等待任務,則不需要在Task.Delay()
調用Task.Delay()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.