2016-06-18 8 views
-1

ファイルをダウンロードしているフォームがあります。私は、ファイルのダウンロードや進行状況などを処理する別のクラス(download.cs)を持っています。完了したらファイルを実行する方法を理解できません。一度ダウンロードしたイベントを見ることができますが、その時点で私はコールルーチンで何が起きているのか再開したいと思います。明確にするためダウンロードしたファイルをクラスからダウンロードします。

のForm1.cs

private void btnDownloadProfile_Click(object sender, EventArgs e) 
      { 
       tc.SelectTab("tabLog"); 
       Download d = new Download(); 
       d.DownloadFile("http://www.****.com/Downloads/***.zip", appPath + @"Files\***.zip", "Filename"); 
       //At this point i'd like to wait for the download to finish and then execute the download 
      } 

Download.cs

WebClient webClient; 
     public void DownloadFile(String URL, String Path, String Name) 
     { 
      using (webClient = new WebClient()) 
      { 
       webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
       webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 
       Uri Link = new System.Uri(URL); 
       try 
       { 
        Log.addLog("Attempting to download: " + Name); 
        webClient.DownloadFileAsync(Link, Path);  
       } 
       catch (Exception ex) 
       { 
        Log.addLog(ex.Message); 
       } 
      } 
     } 

     private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
     { 
      Log.addLog(e.ProgressPercentage.ToString() + "%"); 
     } 

     private void Completed(object sender, AsyncCompletedEventArgs e) 
     { 
      if (e.Cancelled == true) 
      { 
       Log.addLog("Download has been canceled."); 
      } 
      else 
      { 
       Log.addLog("Download completed!"); 
      } 
     } 

EDIT 。ボタンをクリックすると、イベントはForm1.csで実行され、ダウンロードが開始されます。これは、非同期のものを使用して背景に移動し、UIを自由にします。ログファイルにダウンロードが表示されます。 CompeltedアイテムがDownload.csで実行されると、ファイルのダウンロードが完了したことがわかります。しかし、私はその時点でどのファイルがダウンロードを完了したのかわからない。ファイルがダウンロードされたことを伝えるだけだ。どのようにしてファイル名を完成させるか、btnDownloadProfile_Clickのイベントをファイルが終了してから処理するまで続けることができます。

+0

ダウンロード後にZIPファイルを開きたいだけですか? あなたはプログラムを使うか、デフォルトのプログラムを実行してzipファイルを開きますか? 両方の解決策があります:System.IO.Compression もう1つはSystem.Diagnostics.Process.Start –

+0

こんにちは、申し訳ありませんが、私はダウンロードしたファイルを抽出または実行する方法を知っています。私が得られないことは、完了したルーチンがダウンロードされた後、ファイルの詳細などを取得してフォーム1のサブルーチンを続けることです。 – TMB87

+0

私は理解できません:c質問を編集して、 3? –

答えて

0

私の知る限り理解し、あなたがファイルをダウンロードして、あなたがダウンロード方式と呼ばれている場所を継続したい...これが正しければ、あなたは非同期でTaskCompletionSourceクラスを使用することができます/ BTW

を(待ちます:

async void YourMethod() 
{ 

    var dl = new SODownloader(); 
    await dl.DownloadFile(........); 
    //Do whatever you want to do. Download is finsished....    
} 

あなたの修正ダウンローダクラス

は、ダウンロードの進行状況を示すことは必要でない場合は、単に)

使い方をDownloadFileTaskAsyncを使用することができます

using System; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Net; 
using System.Threading.Tasks; 

namespace SO3 
{ 
    public class SODownloader 
    { 
     WebClient webClient; 
     TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); //<----- 

     public Task DownloadFile(String URL, String Path, String Name) 
     { 

      using (webClient = new WebClient()) 
      { 
       webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
       webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 
       Uri Link = new System.Uri(URL); 
       try 
       { 
        Debug.WriteLine("Attempting to download: " + Name); 
        webClient.DownloadFileAsync(Link, Path); 
       } 
       catch (Exception ex) 
       { 
        Debug.WriteLine(ex.Message); 
       } 
      } 

      return tcs.Task; //<----- 
     } 

     private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
     { 
      Debug.WriteLine(e.ProgressPercentage.ToString() + "%"); 
     } 

     private void Completed(object sender, AsyncCompletedEventArgs e) 
     { 

      if (e.Cancelled == true) 
      { 
       tcs.TrySetException(new TaskCanceledException("Download has been canceled.")); //<----- 
       Debug.WriteLine("Download has been canceled."); 
      } 
      else 
      { 
       tcs.TrySetResult("Download completed!"); //<----- 
       Debug.WriteLine("Download completed!"); 
      } 
     } 
    } 
} 
+0

これは私には意味があるようです - 私が得る唯一の問題はエラーです\t CS1061 \t 'タスク'に 'GetAwaiter'の定義が含まれておらず、 'タスク'タイプの最初の引数を受け入れる拡張メソッド 'GetAwaiter'がありませんd = new Download();をダウンロードしてください。 d.DownloadFile( "http://www.***.com/Downloads/***.zip"、appPath + @ "Files \ ***。zip"、 "Profile Wizzard")を待ちます。 – TMB87

+0

@Tom投稿する前にテストしたので、これ以上は何も言えません.Net> = 4.5ですか? – Eser

+0

Aha! .net 4 - アップグレードされ、今すぐ動作します。おかげさまで、本当に有益なことを知っています – TMB87

関連する問題