Guest User

Untitled

a guest
Jun 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. // Looking for basically this functionality:
  2.  
  3. Action cont1 = ...;
  4. Action cont2 = ...;
  5.  
  6. var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
  7.  
  8. Task.Factory.StartNew(() =>
  9. {
  10. if (some-synchronous-computation())
  11. Task.Factory.StartNew(cont1, ..., scheduler);
  12. else
  13. Task.Factory.StartNew(cont2, ..., scheduler);
  14. });
  15.  
  16. // But then without the lambda needing to close over the continuation delegates and especially the TaskScheduler.
  17. // That is to say, I want something much more like this:
  18.  
  19. var task = Task.Factory.StartNew(() =>
  20. {
  21. if (some-synchronous-computation())
  22. throw new Exception();
  23. else
  24. return;
  25. });
  26.  
  27. task.ContinueWith(cont1, TaskContinuationOptions.OnlyOnFaulted,
  28. TaskScheduler.FromCurrentSynchronizationContext());
  29. task.ContinueWith(cont2, TaskContinuationOptions.NotOnFaulted,
  30. TaskScheduler.FromCurrentSynchronizationContext());
  31.  
  32. // But then without the arbitrary exception throwing that really has no place in the code.
  33. // What would you suggest as an elegant way to deal with this?
Add Comment
Please, Sign In to add comment