cancellationtokensource

Difference between CancellationTokenSource and exit flag for Task loop exit

孤人 提交于 2019-11-27 05:59:09
问题 I was wondering if there is any difference between ending loop task with CancellationTokenSource and exit flag CancellationTokenSource: CancellationTokenSource cancellationTokenSource; Task loopTask; void StartLoop() { cancellationTokenSource = new CancellationTokenSource(); loopTask = Task.Factory.StartNew(Loop, TaskCreationOptions.LongRunning); } void Loop() { while (true) { if (cancellationTokenSource.IsCancellationRequested) break; Thread.Yield(); } } void StopLoop() {

Why CancellationToken is separate from CancellationTokenSource?

心已入冬 提交于 2019-11-26 23:56:14
问题 I'm looking for a rationale of why .NET CancellationToken struct was introduced in addition to CancellationTokenSource class. I understand how the API is to be used, but want to also understand why it is designed that way. I.e., why do we have: var cts = new CancellationTokenSource(); SomeCancellableOperation(cts.Token); ... public void SomeCancellableOperation(CancellationToken token) { ... token.ThrowIfCancellationRequested(); ... } instead of directly passing CancellationTokenSource around

Canceling SQL Server query with CancellationToken

孤人 提交于 2019-11-26 21:18:11
问题 I have a long-running stored procedure in SQL Server that my users need to be able to cancel. I have written a small test app as follows that demonstrates that the SqlCommand.Cancel() method works quite nicely: private SqlCommand cmd; private void TestSqlServerCancelSprocExecution() { TaskFactory f = new TaskFactory(); f.StartNew(() => { using (SqlConnection conn = new SqlConnection("connStr")) { conn.InfoMessage += conn_InfoMessage; conn.FireInfoMessageEventOnUserErrors = true; conn.Open();

Using CancellationToken for timeout in Task.Run does not work [duplicate]

a 夏天 提交于 2019-11-26 20:07:31
问题 This question already has answers here : How to cancel a Task in await? (4 answers) Closed 17 hours ago . OK, my questions is really simple. Why this code does not throw TaskCancelledException ? static void Main() { var v = Task.Run(() => { Thread.Sleep(1000); return 10; }, new CancellationTokenSource(500).Token).Result; Console.WriteLine(v); // this outputs 10 - instead of throwing error. Console.Read(); } But this one works static void Main() { var v = Task.Run(() => { Thread.Sleep(1000);

Cancelling an HttpClient Request - Why is TaskCanceledException.CancellationToken.IsCancellationRequested false?

帅比萌擦擦* 提交于 2019-11-26 16:05:44
问题 Given the following code: var cts = new CancellationTokenSource(); try { // get a "hot" task var task = new HttpClient().GetAsync("http://www.google.com", cts.Token); // request cancellation cts.Cancel(); await task; // pass: Assert.Fail("expected TaskCanceledException to be thrown"); } catch (TaskCanceledException ex) { // pass: Assert.IsTrue(cts.Token.IsCancellationRequested, "expected cancellation requested on original token"); // fail: Assert.IsTrue(ex.CancellationToken