private void StartTask <TArg>(Func <TArg, Task> action, TArg arg, MarshaledTaskCompletionSource tcs) { try { SetupTask(action(arg), tcs); } catch (Exception e) { tcs.SetException(e); } }
private void SetupTask(Task task, MarshaledTaskCompletionSource tcs) { task.ContinueWith(t => { if (t.IsFaulted) { tcs.SetException(t.Exception.InnerException); } else { tcs.SetResult(); } }, TaskScheduler.Default); }