本文整理汇总了C#中this.ContinueWith方法的典型用法代码示例。如果您正苦于以下问题:C# this.ContinueWith方法的具体用法?C# this.ContinueWith怎么用?C# this.ContinueWith使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.ContinueWith方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteContinueWithInternal
public static void ExecuteContinueWithInternal(this Task task,Action successAction,Action exceptionAction,
Action cancellationAction)
{
task.ContinueWith(p => successAction(),
TaskContinuationOptions.OnlyOnRanToCompletion);
task.ContinueWith(p => exceptionAction(), TaskContinuationOptions.NotOnFaulted);
task.ContinueWith(p => cancellationAction(),
TaskContinuationOptions.OnlyOnCanceled);
}
示例2: AsAsyncResult
/// <summary>
/// A helper method for mocking APM (classic) asynchronous methods with on TAP (modern) asynchronous methods.
/// </summary>
/// <remarks>
/// This is based on <a href="http://blogs.msdn.com/b/pfxteam/archive/2011/06/27/10179452.aspx"/>
/// and <a href="http://msdn.microsoft.com/en-us/library/hh873178.aspx"/>.
/// </remarks>
public static IAsyncResult AsAsyncResult(this Task task, AsyncCallback callback, object state)
{
Debug.Assert(task != null, "task");
var taskCompletionSource = new TaskCompletionSource<object>(state);
task.ContinueWith(
t =>
{
if (t.IsFaulted)
{
taskCompletionSource.TrySetException(t.Exception.InnerExceptions);
}
else if (t.IsCanceled)
{
taskCompletionSource.TrySetCanceled();
}
else
{
taskCompletionSource.SetResult(null);
}
if (callback != null)
{
callback(taskCompletionSource.Task);
}
},
TaskScheduler.Default);
return taskCompletionSource.Task;
}
示例3: ToApm
internal static Task ToApm(this Task task, AsyncCallback callback, object state) {
if (task == null) {
throw new ArgumentNullException("task");
}
var tcs = new TaskCompletionSource<object>(state);
task.ContinueWith(
t => {
if (t.IsFaulted) {
tcs.TrySetException(t.Exception.InnerExceptions);
} else if (t.IsCanceled) {
tcs.TrySetCanceled();
} else {
tcs.TrySetResult(null);
}
if (callback != null) {
callback(tcs.Task);
}
},
CancellationToken.None,
TaskContinuationOptions.None,
TaskScheduler.Default);
return tcs.Task;
}
示例4: IgnoreExceptions
// The unobserved task handler extention. simply observes the exception and do nothing.
public static Task IgnoreExceptions(this Task task)
{
task.ContinueWith(c => { var ignored = c.Exception; },
TaskContinuationOptions.OnlyOnFaulted |
TaskContinuationOptions.ExecuteSynchronously);
return task;
}
示例5: ToApm
/// <summary>
/// A Task extension method that converts this object to an IAsyncResult.
/// </summary>
/// <remarks>
/// Mark, 19/06/2012.
/// Props to Stephen Toub for this blog post:
/// http://blogs.msdn.com/b/pfxteam/archive/2011/06/27/10179452.aspx
/// </remarks>
/// <param name="task"> The task to act on.</param>
/// <param name="callback">The callback.</param>
/// <param name="state">The state.</param>
/// <returns>
/// The given data converted to an IAsyncResult-y Task.
/// </returns>
public static Task ToApm(this Task task, AsyncCallback callback, object state)
{
task = task ?? MakeCompletedTask();
var tcs = new TaskCompletionSource<object>();
task.ContinueWith(t =>
{
if (t.IsFaulted)
{
tcs.TrySetException(t.Exception.InnerExceptions);
}
else if (t.IsCanceled)
{
tcs.TrySetCanceled();
}
else
{
tcs.TrySetResult(null);
}
if (callback != null)
{
callback(tcs.Task);
}
}, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default);
return tcs.Task;
}
示例6: Forget
public static void Forget(this Task task)
{
task.ContinueWith(t =>
{
var e = t.Exception;
});
}
示例7: Forget
public static void Forget(this Task task)
{
task.ContinueWith(t =>
{
Debugger.Break();
}, TaskContinuationOptions.OnlyOnFaulted);
}
示例8: LogOnException
public static void LogOnException(this Task task)
{
task.ContinueWith(t =>
{
Log.Error("Exception thrown in task started with LogOnException()", t.Exception);
},
TaskContinuationOptions.OnlyOnFaulted);
}
示例9: Ignore
public static void Ignore(this Task task)
{
Contract.Requires(task != null);
// ReSharper disable CSharpWarnings::CS4014
task.ContinueWith(t => t.Exception, TaskContinuationOptions.OnlyOnFaulted);
// ReSharper restore CSharpWarnings::CS4014
}
示例10: Catch
public static Task Catch(this Task task)
{
return task.ContinueWith(t =>
{
if (t.Exception != null)
t.Exception.Handle(OnException);
}, TaskContinuationOptions.OnlyOnFaulted);
}
示例11: OnExceptions
/// <summary>
/// Observes any exceptions thrown by a task and performs an Action on them. (ie: logging). This does not occur on task cancellation. Recommended that you iterate over AggregateException.InnerExceptions in this.
/// </summary>
/// <param name="task"></param>
/// <param name="action"></param>
/// <returns></returns>
public static Task OnExceptions(this Task task, Action<AggregateException> action)
{
task.ContinueWith(t =>
{
action(t.Exception.Flatten());
}, TaskContinuationOptions.OnlyOnFaulted);
return task;
}
示例12: WaitWithPumping
public static void WaitWithPumping(this Task task)
{
if (task == null) throw new ArgumentNullException("task");
var nestedFrame = new DispatcherFrame();
task.ContinueWith(_ => nestedFrame.Continue = false);
Dispatcher.PushFrame(nestedFrame);
task.Wait();
}
示例13: Wait
public static void Wait(this Task t)
{
var e = new System.Threading.ManualResetEvent(false);
t.ContinueWith(_ => e.Set());
e.WaitOne();
}
示例14: FailFastOnException
public static Task FailFastOnException(this Task task)
{
task.ContinueWith(c =>
Environment.FailFast(
"An unhandled exception occurred in a background task", c.Exception),
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
return task;
}
示例15: FireAndForget
public static void FireAndForget(this Task task,string taskName,Action<Task> errorHandler=null)
{
if (errorHandler == null)
{
errorHandler = t => taskName.LogError(t.Exception);
}
task.ContinueWith(errorHandler, TaskContinuationOptions.OnlyOnFaulted);
}