0

I am trying to download a large file(around 1GB) my server.When I start downloading I am unable to use the app till the download completes. It is blocking the UI and its becoming unresponsive.

In below code I am calling DownloadFile method when the user download button on the UI.And then download starts, but the UI is freezed.

I read that DownloadFileAsync won't block the UI. But here its blocking. How to use it in correct way. There are several answers but none is working when I am testing.

Code:

Button call:

private void Button_Click(object sender, RoutedEventArgs e)
{
        Debug.WriteLine("1");           
        DownloadGamefile DGF = new DownloadGamefile();
        Debug.WriteLine("2" + Environment.CurrentDirectory);
        DGF.DownloadFile("URL(https link to zip file)", Environment.CurrentDirectory + @"\ABC.zip");
        Debug.WriteLine("3");
 }

Download code:

 class DownloadGamefile
{
    private volatile bool _completed;

    public void DownloadFile(string address, string location)
    {
        WebClient client = new WebClient();
        Uri Uri = new Uri(address);
        _completed = false;

        client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
        client.DownloadFileAsync(Uri, location);

    }

    private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
    {
        // Displays the operation identifier, and the transfer progress.
        Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",
            (string)e.UserState,
            e.BytesReceived,
            e.TotalBytesToReceive,
            e.ProgressPercentage);
    }

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

        _completed = true;
    }
}
djkpA
  • 1,224
  • 2
  • 27
  • 57
  • How do you know `DownloadFileAsync` is causing your UI to freeze? What are you using `DownloadCompleted` for? I bet it's something to do with that. – Ilian May 04 '17 at 05:05
  • As of now I just wrote it, But not using it any where.Edited Code. – djkpA May 04 '17 at 05:08
  • The only way that `DownloadFileAsync` could be blocking the UI thread is if it doesn't return until the file has finished downloading. Is that what's happening? If you place a breakpoint on that call and then step, does the debugger step to the next line straight away or does it sit on the method call until the file has downloaded? – jmcilhinney May 04 '17 at 05:11
  • I know because I am unable to use the app after clicking the Download btn, untill the download completes – djkpA May 04 '17 at 05:12
  • This code does neither block, nor does it use WPF. Please post a [mcve]. – nvoigt May 04 '17 at 05:16
  • @djkp: Show us how you use `DownloadFile` from you Download button Click handler. You must be doing something else in there. – Ilian May 04 '17 at 05:20
  • You need to await the downloadfileasync call – loneshark99 May 04 '17 at 05:20
  • @loneshark99 That won't explain the behaviour he is seeing. Not awaiting an async method will not block the call. – Ilian May 04 '17 at 05:23
  • I updated with button click function code – djkpA May 04 '17 at 05:25
  • @IlianPinzon ofcourse not. take a look at this thread... http://stackoverflow.com/questions/16866059/webclient-downloadfileasync-blocks-thread – loneshark99 May 04 '17 at 05:31
  • How often is the progress reported... is the progress constantly getting reported? – loneshark99 May 04 '17 at 05:34
  • @loneshark99: according to your link, awaiting the call (as you suggested) won't help at all. the problem is within the progress handler. – Ilian May 04 '17 at 05:34
  • @IlianPinzon i know, I was pointing that link to the guy/girl who posted the question. For you the only comment was ofcourse not. – loneshark99 May 04 '17 at 05:35
  • Yeah constantly – djkpA May 04 '17 at 05:35
  • @djkp if you comment that line the progress line.. does it still block ur UI – loneshark99 May 04 '17 at 05:42
  • @djkp you can add the downloaded bytes as a variable and only report when it downloads a certain percentage by adding the downloaded value. That should solve ur issue. – loneshark99 May 04 '17 at 05:45
  • Yeah currently I am reporting for every 2 seconds , its working :) Thanks – djkpA May 04 '17 at 05:47

1 Answers1

0

Please refer to this link. The actual problem is getting lots of feedback about the number of bytes downloaded(about progress of download process). Take a timer to get progress for every 2 seconds or any time, this solved the problem.

Community
  • 1
  • 1
djkpA
  • 1,224
  • 2
  • 27
  • 57