Skip to content

Instantly share code, notes, and snippets.

Created October 29, 2010 18:18
Show Gist options
  • Save anonymous/654056 to your computer and use it in GitHub Desktop.
Save anonymous/654056 to your computer and use it in GitHub Desktop.
// Looking for basically this functionality:
Action cont1 = ...;
Action cont2 = ...;
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(() =>
{
if (some-synchronous-computation())
Task.Factory.StartNew(cont1, ..., scheduler);
else
Task.Factory.StartNew(cont2, ..., scheduler);
});
// But then without the lambda needing to close over the continuation delegates and especially the TaskScheduler.
// That is to say, I want something much more like this:
var task = Task.Factory.StartNew(() =>
{
if (some-synchronous-computation())
throw new Exception();
else
return;
});
task.ContinueWith(cont1, TaskContinuationOptions.OnlyOnFaulted,
TaskScheduler.FromCurrentSynchronizationContext());
task.ContinueWith(cont2, TaskContinuationOptions.NotOnFaulted,
TaskScheduler.FromCurrentSynchronizationContext());
// But then without the arbitrary exception throwing that really has no place in the code.
// What would you suggest as an elegant way to deal with this?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment