Guest User

Untitled

a guest
Jul 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. //this will work
  2. private void button1_Click(object sender, RoutedEventArgs e)
  3. {
  4. var query = ctx.AccountSet;
  5. query.BeginExecute((result) =>
  6. {
  7. textBox1.Text = query.EndExecute(result).First().Name;
  8. }, null);
  9. }
  10.  
  11. //this will fail
  12. private void button2_Click(object sender, RoutedEventArgs e)
  13. {
  14. System.Threading.Tasks.Task.Factory.StartNew(RestAsync);
  15. }
  16.  
  17. void RestAsync()
  18. {
  19. var query = ctx.AccountSet;
  20. var async = query.BeginExecute(null, null);
  21. var task = System.Threading.Tasks.Task.Factory.FromAsync<Account>(async, (result) =>
  22. {
  23. return query.EndExecute(result).First(); // <- Exception thrown here
  24. });
  25. textBox1.Dispatcher.BeginInvoke(() =>
  26. {
  27. textBox1.Text = task.Result.Name;
  28. });
  29. }
  30.  
  31. private static AsyncCallback SyncContextCallback(AsyncCallback callback)
  32. {
  33. SynchronizationContext sc = SynchronizationContext.Current;
  34. // If there is no SC, just return what was passed in
  35. if (sc == null) return callback;
  36. // Return a delegate that, when invoked, posts to the captured SC a method that
  37. // calls the original AsyncCallback passing it the IAsyncResult argument
  38. return asyncResult => sc.Post(result => callback((IAsyncResult)result), asyncResult);
  39. }
  40.  
  41. protected override void OnMouseClick(MouseEventArgs e) {
  42. // The GUI thread initiates the asynchronous Web request
  43. Text = "Web request initiated";
  44. var webRequest = WebRequest.Create("http://Wintellect.com/");
  45. webRequest.BeginGetResponse(SyncContextCallback(ProcessWebResponse), webRequest);
  46. base.OnMouseClick(e);
  47. }
  48.  
  49. private void ProcessWebResponse(IAsyncResult result) {
  50. // If we get here, this must be the GUI thread, it's OK to update the UI
  51. var webRequest = (WebRequest)result.AsyncState;
  52. using (var webResponse = webRequest.EndGetResponse(result)) {
  53. Text = "Content length: " + webResponse.ContentLength;
  54. }
  55. }
  56.  
  57. public override void UpdateCanvas(object parameter)
  58. {
  59. Action<GraphPane> startToUpdate = StartToUpdate;
  60. GraphPane selectedPane = Canvas.HostingPane.PaneList.Find(p => p.Title.Text.Equals(defaultPanTitle));
  61. startToUpdate.BeginInvoke(selectedPane, FormSyncContext.SyncContextCallback(RefreshCanvas), selectedPane);
  62. }
  63.  
  64. public static AsyncCallback SyncContextCallback(AsyncCallback callback)
  65. {
  66. // Capture the calling thread's SynchronizationContext-derived object
  67. SynchronizationContext sc = SynchronizationContext.Current;
  68.  
  69. // If there is no SC, just return what was passed in
  70. if (sc == null) return callback;
  71.  
  72. // Return a delegate that, when invoked, posts to the captured SC a method that
  73. // calls the original AsyncCallback passing it the IAsyncResult argument
  74. return asyncResult => sc.Post(result => callback((IAsyncResult)result), asyncResult);
  75. }
Add Comment
Please, Sign In to add comment