2016-05-17 112 views
1

我目前正在製作一個小型迷你遊戲。遊戲有能力打開一些選項,在選項中你可以點擊「啓用音樂」。由於應用程序包括音頻文件太大,我希望用戶選擇他是否需要音樂,並且必須下載它。使用AsyncTask下載文件並在進度條中顯示進度

我使用的是異步任務此選項,因爲選項應該彈出式同時下載,這是到目前爲止我的代碼:

 public void CheckForFiles() 
    { 
     // searches the current directory and sub directory 
     int fCount = Directory.GetFiles(path, "*", SearchOption.AllDirectories).Length; 
     //If there are NOT all of the Audiofiles, delete all of them and re-download! 
     if (fCount != 2) 
     { 
      //Just to be sure, delete all previous files! 
      Array.ForEach(Directory.GetFiles(@"C:\Users\Public\Documents\Ultimate Tic-Tac-Toe\Audios\"), File.Delete); 
      //Change the Button from "Apply" to "Download" 
      apply_options.Text = "Download"; 
      //Set the Warning Text 
      warning_text.Text = "Warning! Downloading the needed Audiofiles. DO NOT interrupt the Proccess!"; 
      //SHow the Download-Progress Bar 
      download_progress.Visible = true; 
      //Download all 
      WebClient webClient = new WebClient(); 
      webClient.DownloadProgressChanged += (s, y) => 
      { 
       download_progress.Value = y.ProgressPercentage; 
      }; 
      webClient.DownloadFileCompleted += (s, y) => 
      { 
       download_progress.Visible = false; 
       warning_text.Text = "Complete!"; 
       CheckForFiles(); 
      }; 
      webClient.DownloadFileAsync(new Uri(remoteUri_dark), fileName_dark); 
      //Text = "Downloading File one of" + WAITLIST; 
     } 

此下載一個文件,但我需要兩個。 所以我試圖等待我的進度欄填寫,下載下一個,如果我有2個文件,完成!但該代碼跳轉到「DownloadFileCompleted」,所以這是行不通的。我現在正坐在這裏約2個小時左右,擺弄。

如何獲得異步任務創建兩個文件的「Que」,下載它們然後跳轉到「DownloadFileCompleted」並仍顯示進度? 謝謝!

+0

進度可以包括公式中的文件總數(如果你下載10個文件和文件5下載40%,那麼它是54%顯示),並在下載結束文件'X'(在'DownloadFileCompleted'?)中,您可以開始加載另一個文件,直到下載完所有文件。 – Sinatr

回答

3

理解異步等待可能有用的東西可能是Eric Lippert's restaurant metaphor,這很好地解釋了爲什麼您想要使用異步等待以及在哪些情況下不需要。在問題頁面的一半處搜索異步和並行性有什麼區別?

Stephen Cleary explains the basics in this article

約異步等待好處是,你的程序看起來連續的,可以理解爲,如果連續的,而實際上,只要它必須等待的東西你的線程看來看去,如果它可以做其他事情而不是等待。在Eric Lippert的餐廳比喻中:與其等待麪包烘烤後纔開始煮水來泡茶。

您忘記做的一件事是等待下載異步。

public async Task CheckForFiles() 
{ 
    // let your thread do all the things sequentially, until it has to wait for something 
    // you don't need the events, just await for the DownloadAsync to complete 
    download_progress.Visible = true; 
    //Download all 
    WebClient webClient = new WebClient(); 
    await webClient.DownloadFileAsync(new Uri(remoteUri_dark), fileName_dark); 
    // if here, you know the first download is finished 
    ShowProgressFirstFileDownloaded(); 
    await webClient.DownloadFileAsync(/* 2nd file */); 
    // if here: 2nd file downloaded 
    ShowProgess2ndFileDownLoaded(); 
    download_progress.Visible = false; 
} 

如果你願意,你可以同時啓動兩個下載:斯蒂芬克利解釋,這隻能如果你聲明你的函數異步,而返回一個任務來完成。這並不總是更快:

public async Task CheckForFiles() 
{ 
    ... do the preparations 

    //Download all 
    download_progress.Visible = true; 

    WebClient webClient = new WebClient(); 
    var taskDownload1 = webClient.DownloadFileAsync(/* params 1st file */); 
    // do not await yet, start downloading the 2nd file 
    var taskDownload2 = webClient.DownloadFileAsync(/* params 2nd file */); 
    // still do not await, first show the progress 
    ShowProgessDownloadStarted(); 

    // now you have nothing useful to do, await until both are finished: 
    await Task.WhenAll(new Task[] {taskDownload1, taskDownload2}); 
    // if here, both tasks finished, so: 
    download_progress.Visible = false; 
} 
+0

謝謝!你的代碼似乎是我想要的。我得到的唯一錯誤是用'await webClient.DownloadFileAsync(new Uri(remoteUri_dark),fileName_dark);'「不能等待void」。你能幫忙嗎?謝謝! :) –

+0

通常,如果函數具有異步變體,則它是異步等待版本。而不是無效它返回任務,而不是TResult它返回任務。唉WebClient已經有一個DownloadFileAsync。因此他們創建了一個DownloadFileTaskAsync。那個返回一個(可等待的)任務 –

+0

哇,作爲一個從未在此之前使用過AsyncTasks的新手是.. confusing。我如何擺脫錯誤?我簡單地用'DownloadFiles()'調用Method。謝謝! :) –