TASK,英语单词,名词、动词,作名词的意思是“(困难的)任务,工作;(语言教学中的)活动”,作动词的意思是“派给某人(任务);对(手段或能力)要求极高”
如果单独想执行一个任务,忽略错误,此方法比较适合使用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
public static class PublicTaskExtensions
{
static readonly Action<Task> IgnoreTaskContinuation = task => _ = task.Exception;
public static void Ignore(this Task task)
{
if (task.IsCompleted)
{
_ = task.Exception;
}
else
{
task.ContinueWith(IgnoreTaskContinuation,
CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
}
}
}
}
public static class TaskExpand
{
public static void Forget(this Task task)
{
if (!task.IsCompleted || task.IsFaulted)
{
_ = ForgetAwait(task);
}
static async Task ForgetAwait(Task task)
{
try
{
await task.ConfigureAwait(false);
}
catch
{
//ignore
}
}
}
}