cancellationtokensource

Cancel Async Task from a button

旧城冷巷雨未停 提交于 2020-01-14 09:10:35
问题 What I need to do is be able to cancel a task that is running async. I have been searching and cannot seem to wrap my head around it. I just cant seem to discern how it would be implemented into my current setup. Here is my code that fires my task off. Any help on where or how to implement a cancellation token would be greatly appreciated. private async void startThread() { //do ui stuff before starting ProgressLabel.Text = String.Format("0 / {0} Runs Completed", index.Count()); ProgressBar

Correctly cancel async operation and fire it again

北战南征 提交于 2020-01-10 03:15:09
问题 How to handle case, where user might hit the button, which invokes long running async operation, multiple time. My idea was first check if the async operation is running, cancel it and fire it again. So far I have tried to build this kind of functionality using CancellationTokenSource, but it is not working as expected. Some times there is two async operations running, so the "old" async oprations is not cancelled yet when I start new one and this mixes up the resul handling. Any suggestions

NetworkStream ReadAsync and WriteAsync hang infinitelly when using CancellationTokenSource - Deadlock Caused by Task.Result (or Task.Wait)

烂漫一生 提交于 2020-01-06 08:07:29
问题 After reading pretty much every question on Stack Overflow and Microsoft's documentation about NetworkStream, I dont understand what is wrong with my code. The problem I see is that my method GetDataAsync() hangs very often. I call this method from Init Method like so: public MyView(string id) { InitializeComponent(); MyViewModel myViewModel = session.Resolve<MyViewModel>(); //Autofac myiewModel.Init(id); BindingContext = myViewModel; } Above, my View does its initialization, then resolves

WriteAsync with timeout

亡梦爱人 提交于 2019-12-24 16:37:31
问题 I try to code a simple async write with timeout as below and expect the function to throw a TaskCanceledException given a very large buffer and small waitTime. However, this does not happen. WriteAsync will block for many seconds until the write completes. What am I missing? public async void WriteWithTimeout(Stream os, byte[] buf, int waitMs) { CancellationTokenSource tokenSource = new CancellationTokenSource(waitMs); // cancel after waitMs milliseconds. await os.WriteAsync(buf, 0, buf

Error: The operation was canceled

吃可爱长大的小学妹 提交于 2019-12-24 14:03:59
问题 I'm using this code snippet to do an async query with a cancellation token: var _client = new HttpClient( /* some setthngs */ ); _client.GetAsync(someUrl, cancellationToken).ContinueWith(gettingTask => { cancellationToken.ThrowIfCancellationRequested(); SomeStuffToDO(); }, TaskScheduler.FromCurrentSynchronizationContext()); }, TaskScheduler.FromCurrentSynchronizationContext()); But, when operation get cancelled, cancellationToken.ThrowIfCancellationRequested(); throws an exception. I know

Forward the cancellation to the right partition

谁说胖子不能爱 提交于 2019-12-24 11:27:16
问题 This question is continuation from previous thread. store cancellation tokens in service fabric services Opened a new thread because answering this question would not be trivial. To summarize what has been discussed regarding cancel a job / task running in particular service in some partition of service fabric: Cancellation Token limitation: Cancellation token works within the service partition and that is the limitation in partition-wise storing the instances, probably the reason why

Race condition with CancellationToken where CancellationTokenSource is only cancelled on the main thread

限于喜欢 提交于 2019-12-19 03:44:07
问题 Consider a Winforms application, where we have a button that generates some results. If the user presses the button a second time, it should cancel the first request to generate results and start a new one. We're using the below pattern, but we are unsure if some of the code is necessary to prevent a race condition (see the commented out lines). private CancellationTokenSource m_cts; private void generateResultsButton_Click(object sender, EventArgs e) { // Cancel the current generation of

Is code that disposes CancellationTokenSource while tasks are canceling correct?

旧街凉风 提交于 2019-12-18 14:51:45
问题 I see this code in front of me and I am suspicious: CancellationTokenSource _cts; public void Dispose(); { _cts.Cancel(); _cts.Dispose(); _task.Wait(); //wait for the task to be canceled!? } Is it safe to call _cts.Dispose() straight after cancel? Wouldn't that dispose of underlying resources of the CancellationTokenSource that are needed for the task being cancelled to successfully Wait on the CancellationToken, if it wanted to do so? 回答1: Is it safe to call _cts.Dispose() straight after

Is code that disposes CancellationTokenSource while tasks are canceling correct?

隐身守侯 提交于 2019-12-18 14:51:23
问题 I see this code in front of me and I am suspicious: CancellationTokenSource _cts; public void Dispose(); { _cts.Cancel(); _cts.Dispose(); _task.Wait(); //wait for the task to be canceled!? } Is it safe to call _cts.Dispose() straight after cancel? Wouldn't that dispose of underlying resources of the CancellationTokenSource that are needed for the task being cancelled to successfully Wait on the CancellationToken, if it wanted to do so? 回答1: Is it safe to call _cts.Dispose() straight after

Task.Factory.FromAsync with CancellationTokenSource

狂风中的少年 提交于 2019-12-18 12:59:10
问题 I have the following line of code used to read asynchronously from a NetworkStream: int bytesRead = await Task<int>.Factory.FromAsync(this.stream.BeginRead, this.stream.EndRead, buffer, 0, buffer.Length, null); I'd like to make it support cancellation. I see that I can cancel tasks using a CancellationTokenSource, however I don't see any way I can pass it to TaskFactory.FromAsync(). Is it possible to make a FromAsync()-constructed task support cancellation? Edit: I want to cancel a task that