task

Task cancellation best practices

余生颓废 提交于 2020-12-05 05:32:22
问题 Lets say I have a processor who's job is to persist files back to the disk. This is running as a Task while observing a BlockingCollection<T> for files to process. When the task gets cancelled and there are still files which should be saved to the disk, what would be a good practice for doing so? It would be convenient to let the task right before exiting quickly write the files remaining back to the disk although I'm not sure if this conflicts with the philosophy of cancelling a task (since

Why CancellationTokenSource hangs an application

旧城冷巷雨未停 提交于 2020-11-29 02:53:56
问题 Here is simple code piece that hangs and never ends: public static void Main() { using (var cancellationTokenSource = new CancellationTokenSource()) { Console.CancelKeyPress += (_, __) => cancellationTokenSource.Cancel(); while (!cancellationTokenSource.Token.WaitHandle.WaitOne(1000)) { Console.WriteLine("Still running..."); } Console.WriteLine("Cancellation is requested. Trying to dispose cancellation token..."); } Console.WriteLine("Just before close"); } The problem is Just before close

Why CancellationTokenSource hangs an application

假装没事ソ 提交于 2020-11-29 02:51:54
问题 Here is simple code piece that hangs and never ends: public static void Main() { using (var cancellationTokenSource = new CancellationTokenSource()) { Console.CancelKeyPress += (_, __) => cancellationTokenSource.Cancel(); while (!cancellationTokenSource.Token.WaitHandle.WaitOne(1000)) { Console.WriteLine("Still running..."); } Console.WriteLine("Cancellation is requested. Trying to dispose cancellation token..."); } Console.WriteLine("Just before close"); } The problem is Just before close

Why CancellationTokenSource hangs an application

让人想犯罪 __ 提交于 2020-11-29 02:51:14
问题 Here is simple code piece that hangs and never ends: public static void Main() { using (var cancellationTokenSource = new CancellationTokenSource()) { Console.CancelKeyPress += (_, __) => cancellationTokenSource.Cancel(); while (!cancellationTokenSource.Token.WaitHandle.WaitOne(1000)) { Console.WriteLine("Still running..."); } Console.WriteLine("Cancellation is requested. Trying to dispose cancellation token..."); } Console.WriteLine("Just before close"); } The problem is Just before close