cancellationtokensource

How to cancel a running task?

风格不统一 提交于 2019-12-17 21:21:50
问题 I want to cancel a running task (when the users presses the escape key). when i click on "escape" key Form_KeyDown run but doesn't cancel task! CancellationTokenSource tokenSource = new CancellationTokenSource(); CancellationToken token=new CancellationToken(); private async void Search_Button_ClickAsync(object sender, EventArgs e) { token = tokenSource.Token; await (Task.Factory.StartNew(() => { //...my program }, token)); private void Form_KeyDown(object sender, KeyEventArgs e) { if (e

How do I reset a CancellationToken properly?

浪尽此生 提交于 2019-12-17 18:25:22
问题 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 =

How to cancel CSharpScript.RunAsync

坚强是说给别人听的谎言 提交于 2019-12-12 12:32:47
问题 How can I stop RunAsync? CancellatioTokenSource cts = new CancellationTokenSource(); //I thought that it's must work, but it don't var script = CSharpScript.Create(code: someCode); await script.RunAsync(cancelletionToken: cts.Token); void button_click() { cts.Cancel() } How else can I do this. And for why such methods need cancellationToken parameter? 回答1: You must do it before the await call, which blocks execution until yout get results, e.g.: var task = script.RunAsync(cancelletionToken:

task IsCanceled is false, while I canceled

房东的猫 提交于 2019-12-10 15:16:27
问题 When I cancel a Task, the await result still returns true for the IsCanceled Property. Seems something is going wrong. Please advise. This is the code: CancellationTokenSource _cancelLationToken = new CancellationTokenSource(); private async void Button_Click(object sender, EventArgs e) { _cancelLationToken = new CancellationTokenSource(); _cancelLationToken.Token.Register(theCallBack); var myTaskToWaitFor = Task.Factory.StartNew(() => WorkHard(_cancelLationToken.Token), _cancelLationToken

How can a default(CancellationToken) have a corresponding CancellationTokenSource

℡╲_俬逩灬. 提交于 2019-12-10 02:58:11
问题 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

Cancel task by time

天涯浪子 提交于 2019-12-09 03:34:07
问题 I have a multi-threaded application where I need to cancel each task after a certain time, even if at the time of cancellation, they use unmanaged resources. Now I use the following code (for example, a console application). In a real application, the delay may occur in the unmanaged resource. static void Main() { for (int i = 0; i < 10; i++) { Task.Factory.StartNew(Do, TaskCreationOptions.LongRunning); } Console.ReadLine(); } private static void Do() { new Timer(Thread.CurrentThread.Abort,

AsTask doesn't exist to implement a cancellation timeout for Async Read

余生长醉 提交于 2019-12-08 07:34:05
问题 I am eager to add a time-out feature to my stream.ReadAsync() and read Microsoft help in this article Async Cancellation: Bridging between the .NET Framework and the Windows Runtime (C# and Visual Basic). This article suggests to use AsTask() to make this happen. However my C# doesn't seem to recognize AsTask() at all. I am using Visual Studio 2013 with Windows 7 along with using System.Threading; and this is the piece of code I have: private async Task<int> ReadAsync(BluetoothClient Client)

Checking that CancellationTokenSource.Cancel() was invoked with Moq

こ雲淡風輕ζ 提交于 2019-12-07 14:44:03
问题 I have a conditional statement which should looks as follows: //... if(_view.VerifyData != true) { //... } else { _view.PermanentCancellation.Cancel(); } where PermanentCancellation is of type CancellationTokenSource. Im wondering how i should set this up in my mock of _view. All attempts thus far have failed :( and i cant find an example on google. Any pointers would be appreciated. 回答1: Because CancellationTokenSource.Cancel is not virtual you cannot mock it with moq. You have two options:

How to use CancellationTokenSource to close a dialog on another thread?

陌路散爱 提交于 2019-12-07 01:57:06
问题 This is related to my other question How to cancel background printing. I am trying to better understand the CancellationTokenSource model and how to use it across thread boundaries. I have a main window (on the UI thread) where the code behind does: public MainWindow() { InitializeComponent(); Loaded += (s, e) => { DataContext = new MainWindowViewModel(); Closing += ((MainWindowViewModel)DataContext).MainWindow_Closing; }; } which correctly calls the CloseWindow code when it is closed:

How to use CancellationTokenSource to close a dialog on another thread?

爷,独闯天下 提交于 2019-12-05 06:19:22
This is related to my other question How to cancel background printing . I am trying to better understand the CancellationTokenSource model and how to use it across thread boundaries. I have a main window (on the UI thread) where the code behind does: public MainWindow() { InitializeComponent(); Loaded += (s, e) => { DataContext = new MainWindowViewModel(); Closing += ((MainWindowViewModel)DataContext).MainWindow_Closing; }; } which correctly calls the CloseWindow code when it is closed: private void CloseWindow(IClosable window) { if (window != null) { windowClosingCTS.Cancel(); window.Close(