Exemple #1
0
 private static void GetResponse(Uri uri, Action <Response> callback)
 {
     System.Net.WebClient wc = new System.Net.WebClient();
     wc.OpenReadCompleted += (o, a) =>
     {
         if (callback != null)
         {
             DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
             callback(ser.ReadObject(a.Result) as Response);
         }
     };
     wc.OpenReadAsync(uri);
 }
        public static void DeserializeXmlFromUrlAsync <T>(System.Uri uri, OnOpenReadCompleted_t onOpenReadCompleted)
        {
            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                // http://stackoverflow.com/questions/25051674/how-to-wait-for-webclient-openreadasync-to-complete
                client.OpenReadCompleted += new System.Net.OpenReadCompletedEventHandler(onOpenReadCompleted);
                client.OpenReadAsync(uri, "userToken");


                // Lambda:
                //client.OpenReadCompleted += (s, e) =>
                //{

                //    using (System.IO.Stream strm = e.Result)
                //    {

                //    }

                //};


                // Closure:
                //client.OpenReadCompleted += delegate (object sender, System.Net.OpenReadCompletedEventArgs e)
                //{
                //    if (e.Cancelled == true)
                //    {
                //        // MessageBox.Show("Download has been canceled.");
                //        System.Console.WriteLine("Download has been canceled.");
                //        return;
                //    }
                //    else if (e.Error != null)
                //    {
                //        throw e.Error;
                //    }

                //    using (System.IO.Stream strm = e.Result)
                //    {

                //        strm.Close();
                //    }


                //    string userState = (string)e.UserState;
                //    System.Console.WriteLine("UserState: \"{0}\".", userState);
                //};
            }
        }