0

I have a webrequest to get a xml.That works great but when i press F12(lock screen) while the the server is requested by my app...I got a WebException.
I use a taskCompeltionSource object...
Here is my code

public async Task<String> Query(DataRequestParam dataRequestParam)
        {

            _dataRequestParam = dataRequestParam;

            try
            {
                Result = "";
                Result = await myDownloadString(dataRequestParam);

            }

            catch (WebException we)//ERROR IS CAUGHT HERE
            {
                throw new WebException(we.Message);

            }
            catch (Exception ex)
            {
                throw new MyException(ex.Message);

            }

            return Result;
        }

        public static Task<string> myDownloadString(DataRequestParam dataRequestParam)
        {
            var tcs = new TaskCompletionSource<string>();
            var web = new WebClient();

            if (!string.IsNullOrEmpty(dataRequestParam.AuthentificationLogin))
            {
                System.Net.NetworkCredential account = new NetworkCredential(dataRequestParam.AuthentificationLogin, dataRequestParam.AuthentificationPassword);
                web.Credentials = account;
            }

                web.DownloadStringCompleted += (s, e) =>
                {
                    if (e.Error != null) tcs.TrySetException(e.Error);
                    else if (e.Cancelled) tcs.TrySetCanceled();
                    else tcs.TrySetResult(e.Result);
                };

                web.DownloadStringAsync(dataRequestParam.TargetUri);
                return tcs.Task;

        }
Paul Martinez
  • 562
  • 1
  • 9
  • 19
  • 1
    If you haven't disabled ApplisationIdleDetection, your process is stopped while entering Lock screen - thus you probably get the exception. – Romasz Mar 03 '14 at 12:05
  • Thanks i implemented this line : PhoneApplicationService.Current.ApplicationIdleDetectionMode =IdleDetectionMode.Disabled; and it doesn't crash anymore... – Paul Martinez Mar 03 '14 at 13:16
  • You should be aware of some more things while using Idle detection and it also want prevent from exception when hitting Start buton (example) - I've added an answer with some links - maybe it will help. – Romasz Mar 03 '14 at 13:27

1 Answers1

0

If you haven't disabled ApplisationIdleDetection, your process is stopped while entering Lock screen - thus you probably get the exception - like I've said in comment. Disabling will solve this issue, but you must be aware of few things:

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Thank your for these informations...But in my case I dont need to perform downloads during my app is locked. If Web request are processing all I need is to prevent that the app crashes when it becomes locked...The webrequest can finish or not it doesn't matter... I tested with the start button but it doesn't crash when ApplicationIdleDetectionMode is Disabled then I hitted the back button and i came back in my app while the request was still processing... – Paul Martinez Mar 03 '14 at 14:56