task

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

Run same code multiple times in parallel with different parameter

邮差的信 提交于 2021-01-27 05:33:10
问题 This very simple example: int numLanes = 8; var tasks = new List<Task>(); for (var i = 0; i < numLanes; ++i) { var t = new Task(() => { Console.WriteLine($"Lane {i}"); }); tasks.Add(t); } tasks.ForEach((t) => t.Start()); Task.WaitAll(tasks.ToArray()); Produces: Lane 8 Lane 8 Lane 8 Lane 8 Lane 8 Lane 8 Lane 8 Lane 8 Which is not as expected, the parameter i isn't passed correctly. I had thought to use Action<int> to wrap the code but couldn't see how I would. I do not want to write a

Async Await Wait for all the results and continue

无人久伴 提交于 2021-01-07 01:24:34
问题 I'm a bit confuse about how to implement async await approach and wait for results before continuing. I want to make 3 calls to backend in parallel and wait for them until they responds then get the result and assign them internally. Something like this: Private Sub GetParseExpressionResults() If Not isParseExpressionSupported Then Return End If 'Cleaning collections Me.parseExpressionItemsTo.Clear() Me.parseExpressionItemsCC.Clear() Me.parseExpressionItemsSubject.Clear() 'Getting list of

Async Await Wait for all the results and continue

孤者浪人 提交于 2021-01-07 01:24:29
问题 I'm a bit confuse about how to implement async await approach and wait for results before continuing. I want to make 3 calls to backend in parallel and wait for them until they responds then get the result and assign them internally. Something like this: Private Sub GetParseExpressionResults() If Not isParseExpressionSupported Then Return End If 'Cleaning collections Me.parseExpressionItemsTo.Clear() Me.parseExpressionItemsCC.Clear() Me.parseExpressionItemsSubject.Clear() 'Getting list of

c# build a list of tasks before executing

核能气质少年 提交于 2020-12-30 05:30:56
问题 i'm trying to build a list of tasks before executing them. here's some example code: public string Returnastring(string b) { return b; } public string Returnanotherstring(string a) { return a; } private void btnT_Click(object sender, EventArgs e) { bool cont = true; var Returnastringtask = Task.Factory.StartNew(() => Returnastring("hi")); var Returnanotherstringtask = Task.Factory.StartNew(() => Returnanotherstring("bye")); if (cont) { Task.WaitAll(new Task[] { Returnastringtask }); } else {

c# build a list of tasks before executing

六眼飞鱼酱① 提交于 2020-12-30 05:29:52
问题 i'm trying to build a list of tasks before executing them. here's some example code: public string Returnastring(string b) { return b; } public string Returnanotherstring(string a) { return a; } private void btnT_Click(object sender, EventArgs e) { bool cont = true; var Returnastringtask = Task.Factory.StartNew(() => Returnastring("hi")); var Returnanotherstringtask = Task.Factory.StartNew(() => Returnanotherstring("bye")); if (cont) { Task.WaitAll(new Task[] { Returnastringtask }); } else {

Creating a task wrapper around an existing object

≡放荡痞女 提交于 2020-12-25 01:42:22
问题 I have a method which returns a Task where the implementation may or may not need to perform a slow operation in order to retrieve the result. I would like to be able to simply wrap the result value into a Task which is marked as having completed synchronously in the case where the value is already available. Today I have something like this: public Task<Foo> GetFooAsync(int key) { lock(this) { if(_Cache.ContainsKey(key) ) { Task<Foo> ret = new Task<Foo>(()=>_Cache[key]); ret.RunSynchronously

Creating a task wrapper around an existing object

微笑、不失礼 提交于 2020-12-25 01:40:25
问题 I have a method which returns a Task where the implementation may or may not need to perform a slow operation in order to retrieve the result. I would like to be able to simply wrap the result value into a Task which is marked as having completed synchronously in the case where the value is already available. Today I have something like this: public Task<Foo> GetFooAsync(int key) { lock(this) { if(_Cache.ContainsKey(key) ) { Task<Foo> ret = new Task<Foo>(()=>_Cache[key]); ret.RunSynchronously

Is there a way I can cause a running method to stop immediately with a cts.Cancel();

你说的曾经没有我的故事 提交于 2020-12-21 03:52:39
问题 I have code that creates a CancellationTokenSource and that passes it to a method. I have code in another are of the app that issues a cts.Cancel(); Is there a way that I can cause that method to stop immediately without me having to wait for the two lines inside the while loop to finish? Note that I would be okay if it caused an exception that I could handle. public async Task OnAppearing() { cts = new CancellationTokenSource(); await GetCards(cts.Token); } public async Task GetCards

Task cancellation best practices

喜欢而已 提交于 2020-12-05 05:33:42
问题 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