Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 4th, 2012  |  syntax: None  |  size: 3.09 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to write a wrapper around an asynchronous method?
  2. public static void DownloadString(this Uri uri, Action<string> action)
  3. {
  4.     if (uri == null) throw new ArgumentNullException("uri");
  5.     if (action == null) throw new ArgumentNullException("action");
  6.  
  7.     var webclient = new WebClient();
  8.  
  9.     DownloadStringCompletedEventHandler handler = null;
  10.     handler = (s, e) =>
  11.     {
  12.         var result = e.Result;
  13.         webclient.DownloadStringCompleted -= handler;
  14.         webclient.Dispose();
  15.         action(result);
  16.     };
  17.  
  18.     webclient.DownloadStringCompleted += handler;
  19.     webclient.DownloadStringAsync(uri);
  20. }
  21.        
  22. var uri = new Uri("http://someplace.com/books.json");
  23. uri.DownloadString(t =>
  24. {
  25.     // Do something with the string
  26. });
  27.        
  28. public void GetBooks(Uri uri, Action<List<Books>> action)
  29. {
  30.     if (action == null) throw new ArgumentNullException("action");
  31.     uri.DownloadString(t =>
  32.     {
  33.         var books = ParseJSON(t);
  34.         action(books);
  35.     });
  36. }
  37.        
  38. this.GetBooks(new Uri("http://someplace.com/books.json"), books =>
  39. {
  40.     // Do something with `List<Books> books`
  41. });
  42.        
  43. void ParseJSON(string text, Action<List<Books>> action)
  44.        
  45. var uri = new Uri("http://someplace.com/books.json");
  46. uri.DownloadString(t => ParseJSON(t, books =>
  47. {
  48.     // Do something with `List<Books> books`
  49.     // `string t` is also in scope here
  50. }));
  51.        
  52. public static void DownloadString(
  53.     this Uri uri,
  54.     Action<string> action,
  55.     Action<Exception> exception)
  56. {
  57.     if (uri == null) throw new ArgumentNullException("uri");
  58.     if (action == null) throw new ArgumentNullException("action");
  59.  
  60.     var webclient = (WebClient)null;
  61.  
  62.     Action<Action> catcher = body =>
  63.     {
  64.         try
  65.         {  
  66.             body();
  67.         }
  68.         catch (Exception ex)
  69.         {
  70.             ex.Data["uri"] = uri;
  71.             if (exception != null)
  72.             {
  73.                 exception(ex);
  74.             }
  75.         }
  76.         finally
  77.         {
  78.             if (webclient != null)
  79.             {
  80.                 webclient.Dispose();
  81.             }
  82.         }
  83.     };
  84.  
  85.     var handler = (DownloadStringCompletedEventHandler)null;        
  86.     handler = (s, e) =>
  87.     {
  88.         var result = (string)null;
  89.         catcher(() =>
  90.         {  
  91.             result = e.Result;
  92.             webclient.DownloadStringCompleted -= handler;
  93.         });
  94.         action(result);
  95.     };
  96.  
  97.     catcher(() =>
  98.     {  
  99.         webclient = new WebClient();
  100.         webclient.DownloadStringCompleted += handler;
  101.         webclient.DownloadStringAsync(uri);
  102.     });
  103. }
  104.        
  105. public static void DownloadString(this Uri uri, Action<string> action)
  106. {
  107.     uri.DownloadString(action, null);
  108. }
  109.        
  110. var uri = new Uri("http://someplace.com/books.json");
  111. uri.DownloadString(t => ParseJSON(t, books =>
  112. {
  113.     // Do something with `List<Books> books`
  114. }), ex =>
  115. {
  116.     // Do something with `Exception ex`
  117. });
  118.        
  119. ThreadPool.QueueUserWorkItem(DoWork);
  120. public void DoWork(){
  121.     //Just remember that this code happens in a seperate thread so don't update
  122.     //the UI. It will throw an exception. You would need to call
  123.     //Form.BeginInvoke(UpdateFunction) in order to update the UI
  124.     DoSomethingInteresting();
  125. }