Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private readonly CancellationTokenSource s_cts = new CancellationTokenSource();
- public async Task Test()
- {
- Trace.WriteLine("Waiting started.");
- try
- {
- s_cts.CancelAfter(3000);
- await Task.Run(() => Sleep());
- }
- catch (OperationCanceledException ex)
- {
- Trace.WriteLine("Tasks cancelled: timed out.");
- }
- finally
- {
- s_cts.Dispose();
- }
- Trace.WriteLine("Waiting ending.");
- }
- public void Sleep()
- {
- var i = 0;
- while (i < 10)
- {
- Thread.Sleep(1000);
- i += 1;
- Trace.WriteLine(i.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement