代码之家  ›  专栏  ›  技术社区  ›  Paul Martinez

锁定Emulator(WindowsPhone8)屏幕时引发WebException

  •  0
  • Paul Martinez  · 技术社区  · 11 年前

    我有一个获取xml的web请求。这很好,但当我在应用程序请求服务器时按F12(锁定屏幕)。。。我遇到了WebException。
    我使用taskCompletionSource对象。。。
    这是我的代码

    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;
    
            }
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   Romasz    11 年前

    如果你还没有 disabled ApplisationIdleDetection ,您的进程在进入锁定屏幕时停止-因此您可能会遇到异常-就像我在评论中所说的那样。禁用将解决此问题,但您必须注意以下几点: