6

Hello I am working on a WPF platform targeting .NET framework 4.5.2. I am writing a downloader for my application. Here is the code:

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);

    }

Now I want at button click the download must be paused and at another button click the download must be resumed. I tried working with .set() property of an AutoReset event and .Reset() property of the same but it didn't work. I need help. My button click code are:

    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();

    }

I have also tried this link How to pause/suspend a thread then continue it?. nothing happens

I've also gone through Adding pause and continue ability in my downloader but I failed to incorporate the solution in my above code as I am also updating the download progress on the UI.

Community
  • 1
  • 1
  • Have you tried sth like this: http://stackoverflow.com/questions/2430930/how-to-pause-suspend-a-thread-then-continue-it – bakala12 Sep 02 '16 at 08:14
  • @bakala12 yes I have tried the above. No results –  Sep 02 '16 at 08:44
  • Possible duplicate of [Adding pause and continue ability in my downloader](http://stackoverflow.com/questions/15995705/adding-pause-and-continue-ability-in-my-downloader) – slawekwin Sep 02 '16 at 08:55
  • @slawekwin I've gone through the above mentioned link before, I couldn't understand how could I implement it in my above code. As I am also updating the download progress on a progressbar –  Sep 02 '16 at 09:18
  • The point is that `WebClient` does not allow you to pause the download process. If you really need it, you will have to implement it yourself and the linked question shows you how. Progress bar is a separate matter, if you are having problems with it, ask another question. – slawekwin Sep 02 '16 at 09:21
  • @slawekwin No no I am not having two problems, I am running the download process on a different thread. I am failing to modify my code with reference to the example usage. can you please help me out here? what will change in my method once I've provided the FileDownload class. –  Sep 02 '16 at 09:25
  • `FileDownload` class is what you need to implement yourself. You are now downloading the file in two separate threads in fact, one is created by you explicitly, the other one is spawned by `myWebClient.DownloadFileAsync` – slawekwin Sep 02 '16 at 09:36
  • @slawekwin yes, I know I am using two threads this is so such that I can update my UI with the progress. Can you help me with a FileDownload class. All I need to download are three .zip files from a link. The above code downloads those 3 .zip files sequentially. Now I want to add a pause resume capability. I ask for your help as I can't understand how the FileDownload class is working with the UserAgents and chunkSize –  Sep 02 '16 at 10:16
  • Have you checked on this ? http://stackoverflow.com/a/9050262/4610605 – Felix D. Sep 06 '16 at 14:16

1 Answers1

1

Well I did some more digging, apparently if for you Adding pause and continue ability in my downloader wasn't clear as the it uses byte stream data in the class. Maybe you could check out the link below, it also provides a VS solution on WPF for the downloading .zip file extensions with pause/resume/stop capabilities. Please let me know if you need some more help.


Link to CodeProject article: C# .NET Background File Downloader

Community
  • 1
  • 1
iam.Carrot
  • 4,976
  • 2
  • 24
  • 71
  • MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKz+jOPFamFFu04mv77zN9ZY6XXC/0bEaKkEWmlXEhAGEYeoKmKdT5qqBpqSrvPSt91eWr56QQJ5C/qXUJ2SK/kCAwEAAQ== – iam.Carrot Jan 18 '19 at 15:40
  • QopO95UOtVyYroJ+/1i+e3/Pf9qFdqja/+YLzs7FEIkTrbY1ZRPkkzPz7v+HP0Wk82jjgbxZEFmUy2Kc9v7aCA== – iam.Carrot Jan 18 '19 at 15:44
  • Illegal base64 character 20 – iam.Carrot Jan 18 '19 at 15:57
  • "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAk5BsL8IJsHQjLpPezaZNNhQn/Vj9pocbH0Cu92TCwRwgsP8/Bpg0VRbo65QNayHQKoYGrKML5kkOeX+tk7bcnQIDAQABAkAvx3+AJy/D9pa9qhtmMwalD/myynG2zeA2Sldcuz9kDxwROITbr1O/WhluDSdmxY4avG9Dv5VO1kmUuhsCWzlxAiEA4yJ8cx1KL7GXAvhsFugo7x/sNRRhJDLs+VHl3ogwOeMCIQCmUTfgqLxKbu0Xy/0wta2WrMKF4NlSH/1ZsMwkscpXfwIgUp61xC3QUX743mS89KwoT6s+AaTTHRbFKn/G7OZ/HF8CIG/HOjTFcvbbooFR2fK4x9fyeDIeMjDdyxpSloE69HDlAiEAhTik9hBrUM9lAVVDIRUQnLt+B//C+a/QFzh8LKH1J3w= – iam.Carrot Jan 18 '19 at 16:03
  • MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJOQbC/CCbB0Iy6T3s2mTTYUJ/1Y/aaHGx9ArvdkwsEcILD/PwaYNFUW6OuUDWsh0CqGBqyjC+ZJDnl/rZO23J0CAwEAAQ== – iam.Carrot Jan 18 '19 at 16:03
  • T9q+2otjka6aFu7857XbKsca+wZmoNejBsrI2q3gkLz5GUIn4G7z5OTu/WsLpMCiLsRgZ0e/Dioe1Fy3B3uhUA== – iam.Carrot Jan 18 '19 at 16:06