task-parallel-library

Can an ActionBlock contain a state?

蓝咒 提交于 2020-03-24 14:17:36
问题 I'm writing an application that is using TPL dataflow. I'm trying to configure an action block to write to a database. However, I need this action block to perform an initialization step on the first message it receives (note that I must wait for the first message and cannot perform the initialization during the action block creation). Because of this, my action block needs to maintain some sort of state that indicates if its already received the first message. Is it possible for an

How to await the results of an IAsyncEnumerable<Task<T>>, with a specific level of concurrency

风格不统一 提交于 2020-03-21 06:51:09
问题 I have an asynchronous stream of tasks, that is generated by applying an async lambda to a stream of items: IAsyncEnumerable<int> streamOfItems = AsyncEnumerable.Range(1, 10); IAsyncEnumerable<Task<string>> streamOfTasks = streamOfItems.Select(async x => { await Task.Delay(100); return x.ToString(); }) The methods AsyncEnumerable.Range and Select above are provided from the System.Linq.Async package. The result I want is a stream of results, expressed as an IAsyncEnumerable<string> . The

How to await the results of an IAsyncEnumerable<Task<T>>, with a specific level of concurrency

此生再无相见时 提交于 2020-03-21 06:51:02
问题 I have an asynchronous stream of tasks, that is generated by applying an async lambda to a stream of items: IAsyncEnumerable<int> streamOfItems = AsyncEnumerable.Range(1, 10); IAsyncEnumerable<Task<string>> streamOfTasks = streamOfItems.Select(async x => { await Task.Delay(100); return x.ToString(); }) The methods AsyncEnumerable.Range and Select above are provided from the System.Linq.Async package. The result I want is a stream of results, expressed as an IAsyncEnumerable<string> . The

Task.ContinueWith() vs 2 Tasks?

微笑、不失礼 提交于 2020-02-25 06:53:51
问题 When should I use Task task1 = Task.Factory.StartNew (() => {...}) .ContinueWith (ant => Console.Write ("2")); vs Task task1 = Task.Factory.StartNew (() => {... }); Task task2 = task1.ContinueWith (ant => Console.Write ("2")); 回答1: It means the same, except for you'll have a reference to the second task now. You can use the second option if the first task needs some processing before executing the tasks all together. An example is to add another var task3 = task1.ContinueWith() so task two

Task.ContinueWith() vs 2 Tasks?

自闭症网瘾萝莉.ら 提交于 2020-02-25 06:50:05
问题 When should I use Task task1 = Task.Factory.StartNew (() => {...}) .ContinueWith (ant => Console.Write ("2")); vs Task task1 = Task.Factory.StartNew (() => {... }); Task task2 = task1.ContinueWith (ant => Console.Write ("2")); 回答1: It means the same, except for you'll have a reference to the second task now. You can use the second option if the first task needs some processing before executing the tasks all together. An example is to add another var task3 = task1.ContinueWith() so task two

How to consume BlockingCollection<'a>.TryTake in F#

Deadly 提交于 2020-02-24 04:22:35
问题 How do I go about using the TryTake method on a BlockingCollection<'a> passing in a timeout period in milliseconds? Heres the signature: BlockingCollection.TryTake(item: byref, millisecondsTimeout: int) : bool is it possible to use the Tuple method of avoiding passing a ref type like on the Dictionary.TryGet methods? i.e. let success, item = myDictionary.TryGetValue(client) Im struggling with this particular signature, any suggestions would be great. Cheers! 回答1: I believe that you can only

How do I get the result or return value of a Task?

蹲街弑〆低调 提交于 2020-02-23 08:57:32
问题 Can someone explain to me how to return the result of a Task? I currently am trying to do the following but my Tasks are not returning my List that I expect? What is the problem here? static void Main() { List<Task> tasks = new List<Task>(); List<string> sha256_hashes = new List<string>(); List<string> results = new List<string>(); sha256_hashes.Add("hash00"); sha256_hashes.Add("hash01"); sha256_hashes.Add("hash03"); foreach(string sha256 in sha256_hashes) { string _sha256 = sha256; var task

Continuous polling using Tasks

冷暖自知 提交于 2020-02-21 13:35:50
问题 This is something I've always used Threads / BackgroundWorker for, but am trying to migrate over to Task way of doing things. Let's say I have a 3rd party SDK I use to read bytes from a USB port. That read call is blocking and times out after 100 ms if no bytes are read, returning null. It returns immediately if bytes are read, returning byte[] array of read bytes. So I basically need to keep polling over and over, and take action on received bytes, by calling Parsing function. It's a WPF

OperationCanceledException VS TaskCanceledException when task is canceled

核能气质少年 提交于 2020-02-19 09:35:33
问题 The following code creates a task which is being canceled. await expression (case 1) throws System.OperationCanceledException while synchronous Wait() (case 2) throws System.Threading.Tasks.TaskCanceledException (wrapped in System.AggregateException ). using System; using System.Threading; using System.Threading.Tasks; public class Program { public static void Main() { Program.MainAsync().Wait(); } private static async Task MainAsync() { using(var cancellationTokenSource = new

How to throttle multiple asynchronous tasks?

旧街凉风 提交于 2020-02-10 03:18:16
问题 I have some code of the following form: static async Task DoSomething(int n) { ... } static void RunThreads(int totalThreads, int throttle) { var tasks = new List<Task>(); for (var n = 0; n < totalThreads; n++) { var task = DoSomething(n); tasks.Add(task); } Task.WhenAll(tasks).Wait(); // all threads must complete } Trouble is, if I don't throttle the threads, things start falling apart. Now, I want to launch a maximum of throttle threads, and only start the new thread when an old one is