//取消信号
CancellationTokenSource Task_TokenSource;
//暂停信号
ManualResetEvent Task_ResetEvent;
//开启
Task_ResetEvent = new ManualResetEvent(true);
Task_TokenSource = new CancellationTokenSource();
Task.Run(async () => {
while (true)
{
if (Task_TokenSource.Token.IsCancellationRequested)
{ return; }
Task_ResetEvent.WaitOne();
//具体的操作
await Task.Delay(5000);
}
}, Task_TokenSource.Token);
//暂停
Task_ResetEvent.Reset();
//恢复
Task_ResetEvent.Set();
//停止
Task_TokenSource.Cancel();