111,097
社区成员




static void Main(string[] args)
{
Task t = null;
CancellationTokenSource cts=new CancellationTokenSource();
for (int i = 0; i < 5; i++)
{
//按你的要求,如果没有完成,提供强制释放
if (t != null && t.IsCompleted)
{
cts.Cancel();
}
}
Console.ReadKey();
}
System.Threading.SemaphoreSlim _slim = new SemaphoreSlim(1);
async Task test1(CancellationToken token)
{
await _slim.WaitAsync();
Task t1 = Task.Factory.StartNew(() =>
{
}, token);
Task t2 = Task.Factory.StartNew(() =>
{
}, token);
//异步信号锁,不执行完不给其他人进
await Task.WhenAll(new List<Task>() { t1, t2 }).ContinueWith(p => { _slim.Release(); });
}