簡體   English   中英

等待后不執行C#控制台應用程序代碼

[英]C# console application code doesn't execute after await

我試圖做一個webscraper,從html文件中獲取css / js / images的所有下載鏈接。

問題

第一個斷點確實命中,但第二個斷點未命中“ Continue”之后。

在Visual Studio中的圖片

我正在談論的代碼:

  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 Main方法,並且您沒有任何其他非工作線程在玩(因為您的主線程已退出):該過程將終止。 就像是:

static void Main() {
    GetHtml();
}

上面的代碼將在GetHtml返回並且Main方法結束后立即終止該過程,該過程將在第一個不完整的await點處進行。

在當前的C#版本(從C#7.1開始)中,您可以創建一個async Task Main()方法,只要您將GetHtml更改為返回Task ,該方法就可以正確地await GetHtml方法:

async static Task Main() {
    await GetHtml();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM