C# C控制台应用程序代码在等待后不执行
我正在尝试制作一个webscraper,从html文件中获取css/js/图像的所有下载链接 问题 第一个断点确实命中,但第二个断点在命中Continue后未命中 我说的代码是:C# C控制台应用程序代码在等待后不执行,c#,async-await,html-parsing,html-agility-pack,C#,Async Await,Html Parsing,Html Agility Pack,我正在尝试制作一个webscraper,从html文件中获取css/js/图像的所有下载链接 问题 第一个断点确实命中,但第二个断点在命中Continue后未命中 我说的代码是: private static async void GetHtml(string url, string downloadDir) { //Get html data, create and load htmldocument HttpClient httpClient
private static async void GetHtml(string url, string downloadDir)
{
//Get html data, create and load htmldocument
HttpClient httpClient = new HttpClient();
//This code gets executed
var html = await httpClient.GetStringAsync(url);
//This code not
Console.ReadLine();
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
//Get all css download urls
var linkUrl = htmlDocument.DocumentNode.Descendants("link")
.Where(node => node.GetAttributeValue("type", "")
.Equals("text/css"))
.Select(node=>node.GetAttributeValue("href",""))
.ToList();
//Downloading css, js, images and source code
using (var client = new WebClient())
{
for (var i = 0; i <scriptUrl.Count; i++)
{
Uri uri = new Uri(scriptUrl[i]);
client.DownloadFile(uri,
downloadDir + @"\js\" + uri.Segments.Last());
}
}
编辑
我正在从这里调用getHtml方法:
private static void Start()
{
//Create a list that will hold the names of all the subpages
List<string> subpagesList = new List<string>();
//Ask user for url and asign that to var url, also add the url to the url list
Console.WriteLine("Geef url van de website:");
string url = "https://www.hethwc.nl";
//Ask user for download directory and assign that to var downloadDir
Console.WriteLine("Geef locatie voor download:");
var downloadDir = @"C:\Users\Daniel\Google Drive\Almere\C# II\Download tests\hethwc\";
//Download and save the index file
var htmlSource = new System.Net.WebClient().DownloadString(url);
System.IO.File.WriteAllText(@"C:\Users\Daniel\Google Drive\Almere\C# II\Download tests\hethwc\index.html", htmlSource);
// Creating directories
string jsDirectory = System.IO.Path.Combine(downloadDir, "js");
string cssDirectory = System.IO.Path.Combine(downloadDir, "css");
string imagesDirectory = System.IO.Path.Combine(downloadDir, "images");
System.IO.Directory.CreateDirectory(jsDirectory);
System.IO.Directory.CreateDirectory(cssDirectory);
System.IO.Directory.CreateDirectory(imagesDirectory);
GetHtml("https://www.hethwc.nu", downloadDir);
}
如何调用GetHtml?假设这是一个sync主方法,并且由于主线程退出,您没有任何其他非工作线程在运行:进程将终止。比如:
static void Main() {
GetHtml();
}
在GetHtml返回并且Main方法结束后,上述过程将立即终止,这将在第一个未完成的等待点结束
在当前的C 7.1版及以后的版本中,您可以创建一个异步Task Main方法,它允许您正确地等待GetHtml方法,只要您更改GetHtml以返回任务:
如何调用GetHtml?假设这是一个sync主方法,并且由于主线程退出,您没有任何其他非工作线程在运行:进程将终止。比如:
static void Main() {
GetHtml();
}
在GetHtml返回并且Main方法结束后,上述过程将立即终止,这将在第一个未完成的等待点结束
在当前的C 7.1版及以后的版本中,您可以创建一个异步Task Main方法,它允许您正确地等待GetHtml方法,只要您更改GetHtml以返回任务:
异步void是一个众所周知的坏主意。你的方法不值得期待。请改用async Task,并确保在调用方法时等待它。您是从sync main调用此方法吗?async void是一个众所周知的坏主意。你的方法不值得期待。改为使用异步任务,并确保在调用方法时等待它。您是从同步主节点调用此方法吗?如果您使用的是C7.0或更早版本,则可以将GetHtml设为异步任务,保持主节点不变,只需调用GetHtml.Wait;等待GetHtml完成。@MicahSwitzer我仍然觉得调用太脏了。等等:相同,但在一个简单的控制台应用程序中,它应该可以满足他的目的。如果您使用C7.0或之前的版本,您可以将GetHtml设为异步任务,保持Main不变,只需调用GetHtml。等等;等待GetHtml完成。@MicahSwitzer我仍然觉得打电话很肮脏。等等:一样,但在一个简单的控制台应用程序中,对他来说应该没问题。