cancellation-token

How can a default(CancellationToken) have a corresponding CancellationTokenSource

时光怂恿深爱的人放手 提交于 2019-12-05 01:59:36
When I create a default CancellationToken I can see in the debugger that the CancellationToken has a CancellationTokenSource associated with it which is stored in the private m_source field: I am wondering how can that be as for structs the default keyword "will return each member of the struct initialized to zero or null depending on whether they are value or reference types" and CancellationTokenSource is a reference type. CancellationToken does have 2 constructors that set this field however they are irrelevant as default(CancellationToken) doesn't call constructors and new

Is default(CancellationToken) equivalent to CancellationToken.None?

大兔子大兔子 提交于 2019-12-04 23:43:52
Looking at the implementation of CancellationToken.None , it is simply returning default(CancellationToken) . However, I see no reference in CancellationToken 's documentation that the two are equivalent. I'd like to offer an API like this but not until I'm sure it'll always work: Task DoSomething(CancellationToken token = default(CancellationToken)) Is it defined behavior that default(CancellationToken) is the same as CancellationToken.None , or is this just an implementation detail? After filing an issue with corefx, the documentation remarks have been updated to make this a guaranteed

Default parameter for CancellationToken

我是研究僧i 提交于 2019-12-04 09:50:01
问题 I have some async code that I would like to add a CancellationToken to. However, there are many implementations where this is not needed so I would like to have a default parameter - perhaps CancellationToken.None. However, Task<x> DoStuff(...., CancellationToken ct = null) yields A value of type '' cannot be used as a default parameter because there are no standard conversions to type 'System.Threading.CancellationToken' and Task<x> DoStuff(...., CancellationToken ct = CancellationToken.None

What is the use of passing CancellationToken to Task Class constructor?

走远了吗. 提交于 2019-12-03 15:04:22
问题 Here is a sample code that creates a new task that simulates a long running process.There is nothing much on the task as such and purely focuses on the cancelling features.I am using cancellation token to cancel the task and the code works fine for me. CancellationTokenSource CTS= new CancellationTokenSource(); Task<Boolean> PTask = new Task<Boolean>(() => { while (true) { if (!CTS.Token.IsCancellationRequested) { Thread.Sleep(5000); } else{Console.WriteLine("Thread Cancelled");break;} }

Is catching TaskCanceledException and checking Task.Canceled a good idea?

假装没事ソ 提交于 2019-12-03 09:47:07
There are some people on my team who really love coding with async Task . And sometimes they like to use CancellationToken parameters. What I'm unsure about is whether we should as a team be using this style of code (A): async Task<someObject> DoStuff(CancellationToken t) { while (!t.IsCanceled) { try { Task.Delay(5000, t); } catch (AggregateException e) // or is it TaskCanceledException or OperationCanceledException? I don't know? :) { } // poll something, return someObject, or null } return null; } This obviously means the caller probably has to check the cancellation token themselves to

What is the use of passing CancellationToken to Task Class constructor?

烈酒焚心 提交于 2019-12-03 04:46:40
Here is a sample code that creates a new task that simulates a long running process.There is nothing much on the task as such and purely focuses on the cancelling features.I am using cancellation token to cancel the task and the code works fine for me. CancellationTokenSource CTS= new CancellationTokenSource(); Task<Boolean> PTask = new Task<Boolean>(() => { while (true) { if (!CTS.Token.IsCancellationRequested) { Thread.Sleep(5000); } else{Console.WriteLine("Thread Cancelled");break;} } return true; }, CTS.Token, TaskCreationOptions.None); PTask.Start(); Console.WriteLine("Hit Enter to cancel

Default parameter for CancellationToken

血红的双手。 提交于 2019-12-03 04:08:13
I have some async code that I would like to add a CancellationToken to. However, there are many implementations where this is not needed so I would like to have a default parameter - perhaps CancellationToken.None. However, Task<x> DoStuff(...., CancellationToken ct = null) yields A value of type '' cannot be used as a default parameter because there are no standard conversions to type 'System.Threading.CancellationToken' and Task<x> DoStuff(...., CancellationToken ct = CancellationToken.None) Default parameter value for 'ct' must be a compile-time constant Is there any way to have a default

Providing cancellation if polling CancellationToken is not possible

对着背影说爱祢 提交于 2019-12-02 05:03:20
问题 Here's a (silly) example of a method that blocks the caller's thread but does not support cancellation: Public Sub WorkUntil5() Threading.SpinWait.SpinUntil(Function() Now.Hour >= 17) End Sub In the worst case scenario, calling this method takes 17 hours to return. Pretend that I don't have access to the source code of this method. How do I wrap the call in a method that takes a CancellationToken? The goal is to let WorkUntil5() run until cancellation is requested. At that point the call

Usage of Cancellation Token

风流意气都作罢 提交于 2019-12-02 04:28:25
问题 I am trying learn how to cancel Task using cancellation token. Here, I have written a UnitTest for it but I am not getting the way it is working. [TestMethod] public async Task Task_should_not_run_when_token_cancelled_before_its_call() { var cts = new CancellationTokenSource(); var token = cts.Token; cts.Cancel(); Debug.WriteLine("Calling Cancellable Method".ToUpper()); try { await CheckMeCalled(token); } catch (Exception expException) { } } private async Task CheckMeCalled(CancellationToken

Usage of Cancellation Token

橙三吉。 提交于 2019-12-02 01:20:32
I am trying learn how to cancel Task using cancellation token. Here, I have written a UnitTest for it but I am not getting the way it is working. [TestMethod] public async Task Task_should_not_run_when_token_cancelled_before_its_call() { var cts = new CancellationTokenSource(); var token = cts.Token; cts.Cancel(); Debug.WriteLine("Calling Cancellable Method".ToUpper()); try { await CheckMeCalled(token); } catch (Exception expException) { } } private async Task CheckMeCalled(CancellationToken ct) { Debug.WriteLine("Before task delayed".ToUpper()); await Task.Delay(5000); Debug.WriteLine("After