0

I have a WebClient which I use to download a file.
This is my code, in which I have a ProgressDialog and a WebClient to download:

dialog = new ProgressDialog(mContext);
                dialog.SetProgressStyle(Android.App.ProgressDialogStyle.Horizontal);
                dialog.SetCancelable(true);
                dialog.SetCanceledOnTouchOutside(true);

                dialog.Show();// showing a dialog

                string url = "myurl";

                WebClient webClient = new WebClient();
                webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
                dialog.CancelEvent += (s, e) =>
                {
                    webClient.CancelAsync();
       //----------------------- Crashes Here
                };

                try
                {
                    bytes = await webClient.DownloadDataTaskAsync(url);
                }
                catch (TaskCanceledException)
                {
                    return;
                }
                catch (Exception a)
                {
                    return;
                }

How do I cancel the download in the middle?

webClient.CancelAsync();

throws an exception:

Object reference not set to an instance of an object
18
  • 1
    What exception do you get, and what's the stack trace? Commented Jul 3, 2017 at 13:02
  • @StephenCleary "System.NullReferenceException: Object reference not set to an instance of an object" Commented Jul 3, 2017 at 13:11
  • Please post enough code to reproduce the problem. Commented Jul 3, 2017 at 13:14
  • @StephenCleary edited with full stack trace. Commented Jul 3, 2017 at 13:16
  • 1
    Have you tried to use a private variable for webClient or better a private locked singleton ? Commented Jul 3, 2017 at 13:40

1 Answer 1

2

The problem was in exception handling code when inner exception was null. To make it work just checking for inner exception using "?"

dialog.CancelEvent += (s, e) =>
                    {
                        webClient.CancelAsync();
                    };

                    try
                    {
                        bytes = await webClient.DownloadDataTaskAsync(url);
                    }
                    catch (WebException wex)
                    {
                        if (wex.Status == WebExceptionStatus.RequestCanceled)
                            return;
                        Toast.MakeText(mContext, wex.Message + "," + wex?.InnerException?.Message, ToastLength.Long).Show();
                        dialog.Progress = 0;
                        return;
                    }
                    catch (TaskCanceledException)
                    {
                        return;
                    }
                    catch (Exception a)
                    {
                        Toast.MakeText(mContext, a.Message + "," + a?.InnerException?.Message, ToastLength.Long).Show();
                        dialog.Progress = 0;
                        return;
                    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.