cancellation-token

Cancel long running Task after 5 seconds when not finished

北战南征 提交于 2021-01-27 19:21:19
问题 I have created a task which creates an XML string. The task can last multiple seconds. When the task isn't finished after 5 seconds, I want to cancel the Task 'smootly' and continue with writing the rest of the XML. So I built in cancellation inside my task. But although I see the following message in the Log: ProcessInformationTask timed out I also see this line in my log Adding the process information took 10001 ms I wonder why this can happen because I want to cancel the Task after 5

cancellation token in F# threads

只谈情不闲聊 提交于 2021-01-24 10:46:06
问题 I am struggling with the following: I have a system that has its main flow and has two background threads that can be started and stopped, but they're generally very long running as they stop only during configuration changes. I found that the cancellation token in F# gets checked at async points in the code. The worker threads do not perform any async operations; they are doing background work but nothing is asynchronous. A simplified version looks like that: let workerThread (someParameters

Get Task CancellationToken

痞子三分冷 提交于 2020-01-01 03:57:04
问题 Can I get CancellationToken which was passed to Task constructor during task action executing. Most of samples look like this: CancellationTokenSource cts = new CancellationTokenSource(); CancellationToken token = cts.Token; Task myTask = Task.Factory.StartNew(() => { for (...) { token.ThrowIfCancellationRequested(); // Body of for loop. } }, token); But what if my action is not lambda but a method placed in other class and I don't have direct access to token ? Is the only way is to pass

CancellationToken with async Dapper methods?

为君一笑 提交于 2019-12-31 17:51:13
问题 I'm using Dapper 1.31 from Nuget. I have this very simple code snippet, string connString = ""; string query = ""; int val = 0; CancellationTokenSource tokenSource = new CancellationTokenSource(); using (IDbConnection conn = new SqlConnection(connString)) { conn.Open(); val = (await conn.QueryAsync<int>(query, tokenSource.Token)).FirstOrDefault(); } When I press F12 on QueryAsync , it points me to public static Task<IEnumerable<T>> QueryAsync<T> ( this IDbConnection cnn, string sql, dynamic

Stopping a Thread, ManualResetEvent, volatile boolean or cancellationToken

给你一囗甜甜゛ 提交于 2019-12-29 07:13:09
问题 I have a Thread (STAThread) in a Windows Service, which performs a big amount of work. When the windows service is restarted I want to stop this thread gracefully. I know of a couple of ways A volatile boolean ManualResetEvent CancellationToken As far as I have found out Thread.Abort is a no go... What is the best practice ? The work is perfomed in another class than the one where the thread is started, so it is necessary to either introduce a cancellationToken parameter in a constructor or

Can i cancel StreamReader.ReadLineAsync with a CancellationToken?

本小妞迷上赌 提交于 2019-12-29 05:52:45
问题 When I cancel my async method with the following content by calling the Cancel() method of my CancellationTokenSource , it will stop eventually. However since the line Console.WriteLine(await reader.ReadLineAsync()); takes quite a bit to complete, I tried to pass my CancellationToken to ReadLineAsync() as well (expecting it to return an empty string) in order to make the method more responsive to my Cancel() call. However I could not pass a CancellationToken to ReadLineAsync() . Can I cancel