C# 多次跳过任务
我想多次跳过几个长时间运行的任务。但现在,我只能发射一次C# 多次跳过任务,c#,task,cancellationtokensource,C#,Task,Cancellationtokensource,我想多次跳过几个长时间运行的任务。但现在,我只能发射一次CancellationTokenSource 我想要达到的目标: 设置外部温度过程并等待温度稳定下来。这一过程需要相当长的时间(约1-2小时)。当我等待的时候,我有时想测量当前的温度并在GUI上给出反馈。这是我在几个温度下做的。在某些情况下,我想手动跳过结算过程。因此,我在GUI上实现了一个按钮,请求取消结算任务(除了一般取消) 我所做的: for(int aa = 0; aa < 10; aa++) { _Cts.Toke
CancellationTokenSource
我想要达到的目标:
设置外部温度过程并等待温度稳定下来。这一过程需要相当长的时间(约1-2小时)。当我等待的时候,我有时想测量当前的温度并在GUI上给出反馈。这是我在几个温度下做的。在某些情况下,我想手动跳过结算过程。因此,我在GUI上实现了一个按钮,请求取消结算任务(除了一般取消)
我所做的:
for(int aa = 0; aa < 10; aa++)
{
_Cts.Token.ThrowIfCancellationRequested();
//To some stuff
//Set and measure temperature
try
{
var internalCTS = new CancellationTokenSource();
Task.WhenAny(
Task.Run(() =>
{
//Skip wait time when cancellation is requested
Task.Delay(TimeSpan.FromMinutes(60)).Wait(_TemperatureCTS.Token);
internalCTS.Cancel();
}),
Task.Run(() =>
{
do
{
//Measure and log temperature
Task.Delay(TimeSpan.FromSeconds(1)).Wait(internalCTS.Token);
}
while(!(internalCTS.IsCancellationRequested || _TemperatureCTS.IsCancellationRequested || _Cts.IsCancellationRequested));
}))
.Wait(_Cts.Token);
}
catch(OperationCanceledException)
{
if(_Cts.Token.IsCancellationRequested)
{
_TemperatureCTS.Cancel();
_Cts.Token.ThrowIfCancellationRequested();
}
}
}
for(int aa=0;aa<10;aa++)
{
_Cts.Token.ThrowIfCancellationRequested();
//一些东西
//设置和测量温度
尝试
{
var internalCTS=新的CancellationTokenSource();
Task.whenay(
Task.Run(()=>
{
//请求取消时跳过等待时间
Task.Delay(TimeSpan.frommins(60)).Wait(_temperatureects.Token);
internalCTS.Cancel();
}),
Task.Run(()=>
{
做
{
//测量并记录温度
Task.Delay(TimeSpan.FromSeconds(1)).Wait(internalCTS.Token);
}
而(!(internalCTS.IsCancellationRequested | | | | | U TemperatureCTS.IsCancellationRequested | | | U Cts.IsCancellationRequested));
}))
.等待(_Cts.Token);
}
捕获(操作取消异常)
{
如果(_Cts.Token.IsCancellationRequested)
{
_温度系数。取消();
_Cts.Token.ThrowIfCancellationRequested();
}
}
}
但我只能跳过一次,因为我无法重置CancellatoinTokenSource。我需要更多类似信息系统的东西。
有什么想法或建议吗?为什么要调用
。等待任务。延迟而不是等待任务。使用异步lambda延迟?为什么要调用任务。延迟而不是等待任务。使用异步lambda延迟?