2016-09-02 17 views
6

こんにちは私は.NET Framework 4.5.2を対象としたWPFプラットフォームで作業しています。私は自分のアプリケーション用のダウンローダを作成しています。ここにコード:ダウンロードの一時停止と再開WPF

private void Download(Dictionary<int, FileAndLinkClass> MyLinks) 
    { 
     ApplicationDownloadThread = new Thread(() => 
     { 
      foreach (KeyValuePair<int, FileAndLinkClass> item in MyLinks) 
      { 
       fileNo++; 
       WebClient myWebClient = new WebClient(); 
       myWebClient.DownloadProgressChanged += MyWebClient_DownloadProgressChanged; 
       myWebClient.DownloadFileCompleted += MyWebClient_DownloadFileCompleted; 
       // Download the Web resource and save it into the current filesystem folder. 
       string downloadedFileAdress = System.IO.Path.Combine(fileLocation, $"{item.Value.FileName}"); 
       myWebClient.DownloadFileAsync(new Uri(item.Value.Link), downloadedFileAdress); 
       while (myWebClient.IsBusy) 
       { 

       } 
      } 
     }); 

     ApplicationDownloadThread.IsBackground = false; 
     ApplicationDownloadThread.Start(); 

     //UnZipAndCreateUpdatePackage(MyLinks); 

    } 

私はボタンをクリックしてダウンロードを一時停止する必要があり、別のボタンをクリックしてダウンロードを再開する必要があります。私は.set() AutoResetイベントのプロパティと同じの.Reset()プロパティで動作しようとしましたが、動作しませんでした。 私は助けが必要です。マイボタンクリックコードは以下のとおりです。

private AutoResetEvent waitHandle = new AutoResetEvent(true); 

    private void StartDownloadBtn_Click(object sender, RoutedEventArgs e) 
    { 
     waitHandle.Set(); 

    } 

    private void StopDownloadBtn_Click(object sender, RoutedEventArgs e) 
    { 
     waitHandle.Reset(); 

    } 

また、私は、このリンクHow to pause/suspend a thread then continue it?を試してみました。何も起こらない

私もAdding pause and continue ability in my downloaderに行ったことがありますが、上記のコードに解決策を組み込むことができませんでした。UIのダウンロードの進行状況も更新しています。

+0

は、あなたがこのようなSTHを試してみました:http://stackoverflow.com/questions/2430930/how-to-pause-suspend-a-thread-then- continue-it – bakala12

+0

@ bakala12はい私は上記を試しました。いいえ結果 –

+0

[私のダウンローダで一時停止と継続能力を追加する]の可能な複製(http://stackoverflow.com/questions/15995705/adding-pause-and-continue-ability-in-my-downloader) – slawekwin

答えて

1

よく分かりましたが、明らかにあなたにとっては、Adding pause and continue ability in my downloaderはクラス内のバイトストリームデータを使用しているため明確ではありませんでした。おそらくあなたは以下のリンクをチェックすることができます、それはまた、一時停止/再開/停止機能を持つダウンロード.zipファイル拡張子のためのWPF上のVSソリューションを提供します。あなたにもう少し助けが必要な場合は教えてください。 CodeProjectの記事へ


リンク: C# .NET Background File Downloader

関連する問題