暂停并恢复下载WPF

6 c# multithreading

您好我正在开发一个针对.NET framework 4.5.2的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);

    }
Run Code Online (Sandbox Code Playgroud)

现在我想按下按钮点击下载必须暂停,在另一个按钮点击下载必须恢复.我尝试使用.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();

    }
Run Code Online (Sandbox Code Playgroud)

我也试过这个链接如何暂停/暂停一个线程然后继续呢?.什么都没发生

我也经历过在我的下载程序中添加暂停和继续功能,但我没有在上面的代码中加入解决方案,因为我还在UI上更新下载进度.

iam*_*rot 1

好吧,我做了一些更多的挖掘,显然,如果您在我的下载器中添加暂停和继续功能还不清楚,因为它在类中使用字节流数据。也许您可以查看下面的链接,它还提供了 WPF 上的 VS 解决方案,用于下载具有暂停/恢复/停止功能的 .zip 文件扩展名。如果您需要更多帮助,请告诉我。


链接到 CodeProject 文章: C# .NET 后台文件下载器