task-parallel-library

How to run a task in max defined parallel threads using c#.net 4.0

≯℡__Kan透↙ 提交于 2021-02-05 06:09:50
问题 I have 1 method that I want to run in 10 different parallel threads, all will be independent there are no dependency on each other, my problem is that if I have 100 items to process and would like to process 10 at a time then how do in run 10 at a time. I have created a sample code where I am using Parallel.ForEach but what do I need to set so it should run 10 threads at a time and suppose any of the the running task have completed then it should automatically take new one, so all 10 threads

How to run a task in max defined parallel threads using c#.net 4.0

孤街醉人 提交于 2021-02-05 06:09:37
问题 I have 1 method that I want to run in 10 different parallel threads, all will be independent there are no dependency on each other, my problem is that if I have 100 items to process and would like to process 10 at a time then how do in run 10 at a time. I have created a sample code where I am using Parallel.ForEach but what do I need to set so it should run 10 threads at a time and suppose any of the the running task have completed then it should automatically take new one, so all 10 threads

What would be a good way to Cancel long running IO/Network operation using Tasks?

半腔热情 提交于 2021-02-04 18:25:17
问题 I've been studying Tasks in .net 4.0 and their cancellation. I like the fact that TPL tries to deal with cancellation correctly in cooperative manner. However, what should one do in situation where a call inside a task is blocking and takes a long time? For examle IO/Network. Obviously cancelling writes would be dangerous. But those are examples. Example: How would I cancel this? DownloadFile can take a long time. Task.Factory.StartNew(() => WebClient client = new WebClient(); client

What would be a good way to Cancel long running IO/Network operation using Tasks?

假装没事ソ 提交于 2021-02-04 18:25:13
问题 I've been studying Tasks in .net 4.0 and their cancellation. I like the fact that TPL tries to deal with cancellation correctly in cooperative manner. However, what should one do in situation where a call inside a task is blocking and takes a long time? For examle IO/Network. Obviously cancelling writes would be dangerous. But those are examples. Example: How would I cancel this? DownloadFile can take a long time. Task.Factory.StartNew(() => WebClient client = new WebClient(); client

APM, EAP and TPL on Socket Programming

我们两清 提交于 2021-02-04 10:57:42
问题 I found Difference between […]Async and Begin[…] .net asynchronous APIs question but this answer confused me a little bit. Talking about these patterns, Stephen said: Most *Async methods (with corresponding *Completed events) are using the Event-Based Asynchronous Pattern. The older (but still perfectly valid) Begin* and End* is a pattern called the Asynchronous Programming Model. The Socket class is an exception to this rule; its *Async methods do not have any corresponding events; it's

Why C# Task.Run() method print the same number in the loop in console app? [duplicate]

天大地大妈咪最大 提交于 2021-01-29 09:49:41
问题 This question already has answers here : How to tell a lambda function to capture a copy instead of a reference in C#? (4 answers) Closed 1 year ago . I have created a console app which print the number in a for loop using C# Task.Run() method. If you look at the code, I am initializing the Employee object in the loop and pass the number in the method, though it prints the same number. Code: class Program { static void Main(string[] args) { for (int i = 1; i <= 500; i++) { Task.Run(() => new

Simple parallelisation for hashset

让人想犯罪 __ 提交于 2021-01-28 19:41:57
问题 I have 2 loops(nested), trying to do a simple parallelisation pseudocode : for item1 in data1 (~100 million row) for item2 in data2 (~100 rows) result = process(item1,item2) // couple of if conditions hashset.add(result) // while adding, incase of a duplicate i also decide wihch one to retain process(item1,item2) to be precise has 4 if conditions bases on values in item1 and item2.(time taken is less than 50ms) data1 size is Nx17 data2 size is Nx17 result size is 1x17 (result is joined into a

Handle exceptions with TPL Dataflow blocks

南楼画角 提交于 2021-01-28 12:10:43
问题 I have a simple tpl data flow which basically does some tasks. I noticed when there is an exception in any of the datablocks, it wasn't getting caught in the initial parent block caller. I have added some manual code to check for exception but doesn't seem the right approach. if (readBlock.Completion.Exception != null || saveBlockJoinedProcess.Completion.Exception != null || processBlock1.Completion.Exception != null || processBlock2.Completion.Exception != null) { throw readBlock.Completion

A simpler way of ignoring specific types of exceptions when awaiting a Task

邮差的信 提交于 2021-01-28 02:24:18
问题 When awaiting a Task I would like to have an easy way to ignore specific types of exceptions, like OperationCanceledException or TimeoutException or whatever. I got the idea of writing an extension method that could wrap my Task , and suppress the Exception type I would give it as argument. So I wrote this one: public static async Task Ignore<TException>(this Task task) where TException : Exception { try { await task; } catch (Exception ex) { if (ex is TException) return; throw; } } I can use

Can I use local variables inside a Parallel Foreach loop (without unintentionally rewriting the previous value)

情到浓时终转凉″ 提交于 2021-01-27 19:05:44
问题 So I am attempting to process records from a datatable one row at a time. I am new to multi-threaded environments and I was asked to use Parallel.Foreach Loop. I wanted to know how local variables are treated in a Parallel execution mode. Following is my code. Parallel.ForEach(dtReportData.Tables[0].AsEnumerable(), drow => { string strType = drow["type"]; //Based on this type, I am executing logic from a switch case. }); Now. I want to know if I can declare and assign value to the variable