task-parallel-library

How should I convert a function returning a non-generic Task to ValueTask?

杀马特。学长 韩版系。学妹 提交于 2021-02-20 06:30:52
问题 I'm working on some code which builds a buffer in memory and then empties it into a TextWriter when the buffer fills up. Most of the time, the character will go straight into the buffer (synchronously) but occasionally (once every 4kb) I need to call TextWriter.WriteAsync . In the System.Threading.Tasks.Extensions package there only appears to be a ValueTask<T> struct, and no non-generic ValueTask (without a type parameter). Why is there no ValueTask , and what should I do if I need to

How should I convert a function returning a non-generic Task to ValueTask?

社会主义新天地 提交于 2021-02-20 06:28:50
问题 I'm working on some code which builds a buffer in memory and then empties it into a TextWriter when the buffer fills up. Most of the time, the character will go straight into the buffer (synchronously) but occasionally (once every 4kb) I need to call TextWriter.WriteAsync . In the System.Threading.Tasks.Extensions package there only appears to be a ValueTask<T> struct, and no non-generic ValueTask (without a type parameter). Why is there no ValueTask , and what should I do if I need to

Parallel.For partitioning

会有一股神秘感。 提交于 2021-02-18 19:42:56
问题 How is partitioning done for something like Parallel.For(0, buffer.Length, (i)=> buffer[i] = 0); My assumption was that for an n core machine, work would be partitioned n way and n threads will carry out the work payload. Which means for example buffer.Length = 100 and n = 4, each thread will get 0-24, 25-49, 50-74, 75-99 blocks. (100 element array is an example to illustrate partitioning, but please consider an array of millions of items.) Is this a fair assumption? Please discuss. I noticed

How to execute each iteration of the loop in one thread with task?

主宰稳场 提交于 2021-02-17 06:53:09
问题 I have a method that check some data and I would like to know how to check this data in differents threads. I am using task and async/await pattern. private bool my myCheckMethod(List<long> paramData) { //code that check the information in the list } private void mainMethod() { //1.- get data from the data base //2.- i group the data from the database in many lists. foreach(list<List<long>> iterator in myListOfLists) { myCheckMethod(itrator); } } But I would like that the myCheckMethod not

TPL Dataflow exception in transform block with bounded capacity

扶醉桌前 提交于 2021-02-15 11:41:53
问题 I need to construct TPL dataflow pipeline which will process a lot of messages. Because there are many messages I can not simply Post them into infinite queue of the BufferBlock or I will face memory issues. So I want to use BoundedCapacity = 1 option to disable the queue and use MaxDegreeOfParallelism to use parallel task processing since my TransformBlock s could take some time for each message. I also use PropagateCompletion to make all completion and fail to propagate down the pipeline.

parallel within parallel code

試著忘記壹切 提交于 2021-02-10 16:25:19
问题 Once I have divided the tasks to 4 independent tasks and make them run in parallel. Is it possible to further make each task run in parallel ? (says, each task, there are lots of for each loop - that could be implemented as parallel code) 回答1: You definitely could, but it doesn't guarantee thread safety. You have to take into account multiple factors though What's the size of your iterations (The more, the better) How many concurrent threads can your CPU handle The amount of cores in your

How to call one task after completion of multiple tasks

独自空忆成欢 提交于 2021-02-10 14:48:51
问题 I have List tasks, these tasks do some irrelevant work. What I need is to show message box in case all the tasks did their work correctly. I tried few ways, but nothing worked properly for me. Simplest way could be use Task continuation = Tasks.WhenAll(tasks); continuation.ContinueWith(obj => { show messagebox }); but ContinueWith still runs for all tasks, not just for continuation task. I tried to set cancellation token or to use sign, but tasks run at same time, so it isn't working. I will

Getting stackoverflow exception with Parallel for each , is this due to thread saftey?

孤街醉人 提交于 2021-02-10 07:51:39
问题 I have a foreach loop which works Well . But I want to implement TPL , so did the following: Parallel.ForEach(fileList, currentfileItem => { _clientContext.Load(currentfileItem, w => w.File); _clientContext.ExecuteQuery(); if (currentfileItem.File == null) { throw new Exception( String.Format("File information not found for the item {0}", currentfileItem.DisplayName)); } var currentFileName = currentfileItem.File.Name; if (!string.IsNullOrEmpty(docRevVersionId)) { var info = Microsoft

BlockingCollection with Parallel.For hangs?

陌路散爱 提交于 2021-02-08 19:43:56
问题 I'm playing around with BlockingCollection to try to understand them better, but I'm struggling to understand why my code hangs when it finishes processing all my items when I use a Parallel.For I'm just adding a number to it (producer?): var blockingCollection = new BlockingCollection<long>(); Task.Factory.StartNew(() => { while (count <= 10000) { blockingCollection.Add(count); count++; } }); Then I'm trying to process (Consumer?): Parallel.For(0, 5, x => { foreach (long value in

BlockingCollection with Parallel.For hangs?

浪子不回头ぞ 提交于 2021-02-08 19:43:53
问题 I'm playing around with BlockingCollection to try to understand them better, but I'm struggling to understand why my code hangs when it finishes processing all my items when I use a Parallel.For I'm just adding a number to it (producer?): var blockingCollection = new BlockingCollection<long>(); Task.Factory.StartNew(() => { while (count <= 10000) { blockingCollection.Add(count); count++; } }); Then I'm trying to process (Consumer?): Parallel.For(0, 5, x => { foreach (long value in