task-parallel-library

TaskCompletionSource : When to use SetResult() versus TrySetResult(), etc

北城余情 提交于 2020-12-29 01:42:40
问题 I'm trying to wrap my head around the TPL, the new async / await features in C# 5, and the mysteries of TaskCompletionSource . One thing that isn't clear to me is when to use SetResult , SetException , and SetCancel versus TrySetResult , TrySetException and TrySetCancel . This is what MSDN has to say: This operation will return false if the Task is already in one of the three final states: RanToCompletion, Faulted, or Canceled. This method also returns false if the underlying Task has already

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

TPL DataFlow One by one processing

☆樱花仙子☆ 提交于 2020-12-13 03:51:20
问题 I am having system that continuously processing messages. I want to make sure that I request messages from an external queue only when previous message was processed. Lets imagine that GetMessages method requests messages from external queue. Got event 1. Will push it Pushed 1 Got event 2. Will push it - my concert is here. As we get item before processing previous Processing 1 Processed 1 Deleted 1 Code: using System; using System.Collections.Generic; using System.Linq; using System

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

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

Asynchronous socket operations in a Task

血红的双手。 提交于 2020-11-30 04:51:13
问题 I have a Threading.Tasks.Task that handles a number of client socket operations (connecting, receiving and sending). I understand that where possible it's best to use non blocking methods using Await because otherwise I'll end up with "parked threads waiting for their response". However, while the Socket class has async methods (SendAsync and so on) they aren't the same as the usual Task Parallel Library async methods, they don't return a Task and cannot be awaited. I realise that I can wrap

Asynchronous socket operations in a Task

余生颓废 提交于 2020-11-30 04:49:54
问题 I have a Threading.Tasks.Task that handles a number of client socket operations (connecting, receiving and sending). I understand that where possible it's best to use non blocking methods using Await because otherwise I'll end up with "parked threads waiting for their response". However, while the Socket class has async methods (SendAsync and so on) they aren't the same as the usual Task Parallel Library async methods, they don't return a Task and cannot be awaited. I realise that I can wrap

Asynchronous socket operations in a Task

a 夏天 提交于 2020-11-30 04:49:33
问题 I have a Threading.Tasks.Task that handles a number of client socket operations (connecting, receiving and sending). I understand that where possible it's best to use non blocking methods using Await because otherwise I'll end up with "parked threads waiting for their response". However, while the Socket class has async methods (SendAsync and so on) they aren't the same as the usual Task Parallel Library async methods, they don't return a Task and cannot be awaited. I realise that I can wrap

Why does assigning a Task then awaiting it allow to run in parallel

你离开我真会死。 提交于 2020-11-29 09:17:13
问题 I've was playing around with async and I've happened across some behaviour I've not noticed before, if this is a duplicate please let me know, but my google-fu has failed me, mostly because I can't think of decent terms to search: Given a simple async method that does some parameterized work: async Task<String> Foo(int i) { await Task.Delay(i); return i.ToString(); } And a calling method to invoke it in different contexts and bundle the result: async Task<Object> Bar() { var one = Foo(3000);