Windows phone 8 网络客户端超时Windows Phone 8
我想在等待web请求期间运行任务。如果任务在请求返回响应之前完成,那么我将显示一条消息“服务器占用的时间太长”。我使用的是Windows phone 8 网络客户端超时Windows Phone 8,windows-phone-8,timeout,webclient,Windows Phone 8,Timeout,Webclient,我想在等待web请求期间运行任务。如果任务在请求返回响应之前完成,那么我将显示一条消息“服务器占用的时间太长”。我使用的是WebClient对象,如何管理超时 public Class Result { protected override void OnNavigatedTo(NavigationEventArgs e) { if (NavigationContext.QueryString.TryGetValue("critere", out sCritere))
WebClient
对象,如何管理超时
public Class Result
{
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.TryGetValue("critere", out sCritere))
{
try
{
_datamanager = new DataManager();
_datamanager.m_evt_Client_DownloadStringCompleted += OnDownloadStringCompleted;
_datamanager.DownloadXmlData(DataManager.URL_RECHERCHE, sCritere);
//HERE I NEED TO RUN A TIMER If the response is too long i would display a message
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Erreur", MessageBoxButton.OK);
NavigationService.GoBack();
NavigationService.RemoveBackEntry();
}
}
}
}
public Class DataManager
{
public void DownloadXmlData(string uri, string critere = "")
{
try
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.Credentials = new NetworkCredential(UserSaved, PasswordSaved, domain);
client.DownloadStringAsync(new Uri(uri + critere));
}
catch(WebException )
{
throw new WebException(MyExceptionsMessages.Webexception) ;
}
catch (Exception )
{
throw new UnknowException(MyExceptionsMessages.UnknownError);
}
}
public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//raise Downloadstringcompleted event if error==null
}
}
您可以使用BackgroundWorker
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (s, e) =>
{
// your task to do while webclient is downloading
};
bw.RunWorkerCompleted += (s, e) =>
{
// check whether DownloadStringCompleted is fired or not
// if not, cancel the WebClient's asynchronous call and show your message.
client.CancelAsync();
MessageBox.Show("message");
}
client.DownloadStringAsync(uri);
bw.RunWorkerAsync();
我发现这个…也许我应该使用任务…看起来更简单更好。。。