当应用程序转到后台时如何处理WebClient调用? [英] How to handle the WebClient call when app goes to background ?

查看:72
本文介绍了当应用程序转到后台时如何处理WebClient调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当应用程序因Windows按钮按下或其他类型的 

I want to Know how to handle the web call when app goes to background due to Windows button press or other type of 

interruption.For eg

interruption.For e.g

     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
     {
     call_Celebrity_Profile_Api(Id, celeb_Name);//method 
     }  

      private async void call_Celebrity_Profile_Api(string celebId, string celebName)
        {
            var obj = new Utility.CelebrityProfile { action = "celebrity_profile", celebrity_id = celebId, searchquery = celebName };

            string json = JsonConvert.SerializeObject(obj, Formatting.Indented);

            if (Utility.IsNetWork())
            {
                try
                {
                    ParseJson(await Utility.Download.GetResponse(json));
                }
                catch (Exception)
                {
                    MessageBox.Show("Sorry, No result found!!");
                    InteractionBlocker.Visibility = Visibility.Collapsed;
                }
            }
            else
            {
                MessageBox.Show("Sorry, your network settings aren't available right now. Please try again.", "Please try again later", MessageBoxButton.OK);
                InteractionBlocker.Visibility = Visibility.Collapsed;
            }
        }

My class for WebClient call is

 public class Downloader
    {
        public System.Threading.Tasks.Task<string> GetResponse(string json)
        {
            try
            {
                var tcs = new System.Threading.Tasks.TaskCompletionSource<string>();
                var webClient = new WebClient();  
                webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
                webClient.UploadStringCompleted += (sender, e1) =>
                    {
                        if (e1.Error == null)
                        {
                            tcs.SetResult(e1.Result);
                        }
                        else
                        {
                            tcs.SetException(e1.Error);
                        }
                    };
                webClient.UploadStringAsync(new Uri("http://admssvc.com/povwebservices/index.php", UriKind.RelativeOrAbsolute), "POST", json);

                return tcs.Task;
            }
            catch (Exception) 
            {
                //if (System.Windows.MessageBox.Show("Sorry, your network settings aren't available right now", "POV", MessageBoxButton.OKCancel) == MessageBoxResult.OK)           
                //{
                //    //(App.Current as App).RootFrame.Navigate(new Uri("/SourceCode/LoginScreen.xaml", UriKind.RelativeOrAbsolute));
                //    (App.Current as App).RootFrame.GoBack();//go back if parsing fails
                //}
                System.Windows.MessageBox.Show("Sorry, your network settings aren't available right now");
                return null;
            }
        }
    }

如果在此过程中该应用程序转到后台  ;由于 

and if  during this process the app goes to background  due to 

由于Windows按钮按下设备或其他类型的 

due to Windows button press of device or other type of 

中断,当应用再次出现在前台时,我收到错误

interruption and when app again comes to foreground I am getting error

Microsoft.Threading.Tasks.dll中出现'System.Net.WebException'类型的第一次机会异常

A first chance exception of type 'System.Net.WebException' occurred in Microsoft.Threading.Tasks.dll

如何处理这种情况。

谢谢




推荐答案

当应用程序停用时,您应该取消所有未完成的Web请求
When the app is deactivated you should cancel all outstanding web requests


这篇关于当应用程序转到后台时如何处理WebClient调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆