cancellation-token

What is “cancellationToken” in the TaskFactory.StartNew() used for?

穿精又带淫゛_ 提交于 2019-11-29 03:28:01
http://msdn.microsoft.com/en-us/library/dd988458.aspx UPD : so, let's discuss this article then: http://msdn.microsoft.com/en-us/library/dd997396.aspx I've changed that code a little: static void Main() { var tokenSource2 = new CancellationTokenSource(); CancellationToken ct = tokenSource2.Token; var task = Task.Factory.StartNew(() => { // Were we already canceled? ct.ThrowIfCancellationRequested(); bool moreToDo = true; Thread.Sleep(5000); while (moreToDo) { // Poll on this property if you have to do // other cleanup before throwing. if (ct.IsCancellationRequested) { Console.WriteLine("exit")

Why CancellationTokenRegistration exists and why does it implement IDisposable

拜拜、爱过 提交于 2019-11-29 02:51:15
问题 I've been seeing code that uses Cancellation.Register with a using clause on the CancellationTokenRegistration result: using (CancellationTokenRegistration ctr = token.Register(() => wc.CancelAsync())) { await wc.DownloadStringAsync(new Uri("http://www.hamster.com")); } I get that you should make sure you Dispose an IDisposable , but why does it even implements IDisposable ? what resources does it have to release? The only methods it has regard equality. What happens if you don't Dispose of

Using CancellationToken for timeout in Task.Run does not work

∥☆過路亽.° 提交于 2019-11-28 20:14:37
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); return 10; }, new CancellationToken(true).Token).Result; Console.WriteLine(v); // this one throws Console.Read(); } Cancellation in Managed Threads : Cancellation is cooperative and is not forced on the

Why is the task is not cancelled when I call CancellationTokenSource's Cancel method in async method?

五迷三道 提交于 2019-11-28 08:13:32
问题 I created a small wrapper around CancellationToken and CancellationTokenSource . The problem I have is that the CancelAsync method of CancellationHelper doesn't work as expected. I'm experiencing the problem with the ItShouldThrowAExceptionButStallsInstead method. To cancel the running task, it calls await coordinator.CancelAsync(); , but the task is not cancelled actually and doesn't throw an exception on task.Wait ItWorksWellAndThrowsException seems to be working well and it uses

How do I reset a CancellationToken properly?

对着背影说爱祢 提交于 2019-11-28 08:01:55
I have been playing round with the async ctp this morning and have a simple program with a button and a label . Click the button and it starts updating the label , stop the button it stops writing to the label . However, I'm not sure how to reset the CancellationTokenSource so that I can restart the process. My code is below: public partial class MainWindow : Window { CancellationTokenSource cts = new CancellationTokenSource(); public MainWindow() { InitializeComponent(); button.Content = "Start"; } async Task DoWork(CancellationToken cancelToken) { int i = 0; while (!cancelToken

Why CancellationToken is separate from CancellationTokenSource?

[亡魂溺海] 提交于 2019-11-28 03:10:23
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 like: var cts = new CancellationTokenSource(); SomeCancellableOperation(cts); ... public void

NetworkStream.ReadAsync with a cancellation token never cancels

依然范特西╮ 提交于 2019-11-27 07:05:57
Here the proof. Any idea what is wrong in this code ? [TestMethod] public void TestTest() { var tcp = new TcpClient() { ReceiveTimeout = 5000, SendTimeout = 20000 }; tcp.Connect(IPAddress.Parse("176.31.100.115"), 25); bool ok = Read(tcp.GetStream()).Wait(30000); Assert.IsTrue(ok); } async Task Read(NetworkStream stream) { using (var cancellationTokenSource = new CancellationTokenSource(5000)) { int receivedCount; try { var buffer = new byte[1000]; receivedCount = await stream.ReadAsync(buffer, 0, 1000, cancellationTokenSource.Token); } catch (TimeoutException e) { receivedCount = -1; } } } I

GetResponseAsync does not accept cancellationToken

谁说胖子不能爱 提交于 2019-11-27 05:16:50
It seems that GetResponseAsync does not accept cancellationToken in Async/Await. So the question is how can I cancel the below procedure, provided I need to collect Cookies from response: using (HttpWebResponse response = (HttpWebResponse) await request.GetResponseAsync()) { cookies.Add(response.Cookies); } An alternative code to achieve the above is also welcome. Something like this should work (untested): public static class Extensions { public static async Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request, CancellationToken ct) { using (ct.Register(() => request.Abort(),

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

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);