parallel.foreach

How to use await in a parallel foreach?

≡放荡痞女 提交于 2019-12-01 01:26:41
问题 So I sepnt the better part of the night trying to figure this out. I was fortunate to get introduced to the parallel.foreach yesterday and it works like I want it to do except from one detail. I have the following: Parallel.ForEach(data, (d) => { try { MyMethod(d, measurements); } catch (Exception e) { // logg } }); Within the method "MyMethod" I have alot of logic that gets done and most of it is fine but I make api calls where I fetch data and I use async task for this to be able to use

How do I collect return values from Parallel.ForEach?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 10:41:25
I'm calling a slow webservice in parallel. Things were great until I realized I need to get some information back from the service. But I don't see where to get the values back. I can't write to the database, HttpContext.Current appears to be null inside of a method called using Parallel.ForEach Below is a sample program (in your mind, please imagine a slow web service instead of a string concatenation) using System; using System.Threading.Tasks; class Program { static void Main(string[] args) { WordMaker m = new WordMaker(); m.MakeIt(); } public class WordMaker { public void MakeIt() { string

Parallel.Foreach SQL querying sometimes results in Connection

冷暖自知 提交于 2019-11-30 10:23:52
I need to speed up performing 12 queries in my application. I switched from a regular foreach to Parallel.ForEach. But sometimes I get an error saying "ExecuteReader requires an open and available connection The connection's current state is connecting." It is my understanding that since many of the 12 queries are using the same InitialCatalog, there is not really a new connection for of the 12 and that may be the problem? How can i fix this? "sql" is a list of type "Sql"- a class is just a string name, string connectiona and a List of queries. Here is the code: /// <summary> /// Connects to

Max Degree of Parallelism for AsParallel()

拟墨画扇 提交于 2019-11-30 00:23:24
问题 While using Parallel.ForEach we have the option to define the Parallel options and set the Max Degree of Parallelism like : Parallel.ForEach(values, new ParallelOptions {MaxDegreeOfParallelism = number}, value = > { // Do Work }) But while doing PLINQ like: Tabel.AsEnumberable() .AsParallel() .Where(//Logic) I was not able to find a way to set MaxDegreeOfParallelism . I looked up on the net as well, but didn't find anything. As anyone found a way around this? Any help is appreciated. 回答1: You

Parallel.Foreach SQL querying sometimes results in Connection

六眼飞鱼酱① 提交于 2019-11-29 15:17:54
问题 I need to speed up performing 12 queries in my application. I switched from a regular foreach to Parallel.ForEach. But sometimes I get an error saying "ExecuteReader requires an open and available connection The connection's current state is connecting." It is my understanding that since many of the 12 queries are using the same InitialCatalog, there is not really a new connection for of the 12 and that may be the problem? How can i fix this? "sql" is a list of type "Sql"- a class is just a

How do I collect return values from Parallel.ForEach?

做~自己de王妃 提交于 2019-11-29 10:32:17
问题 I'm calling a slow webservice in parallel. Things were great until I realized I need to get some information back from the service. But I don't see where to get the values back. I can't write to the database, HttpContext.Current appears to be null inside of a method called using Parallel.ForEach Below is a sample program (in your mind, please imagine a slow web service instead of a string concatenation) using System; using System.Threading.Tasks; class Program { static void Main(string[] args

Parallel.Foreach + yield return?

那年仲夏 提交于 2019-11-29 03:00:03
I want to process something using parallel loop like this : public void FillLogs(IEnumerable<IComputer> computers) { Parallel.ForEach(computers, cpt=> { cpt.Logs = cpt.GetRawLogs().ToList(); }); } Ok, it works fine. But How to do if I want the FillLogs method return an IEnumerable ? public IEnumerable<IComputer> FillLogs(IEnumerable<IComputer> computers) { Parallel.ForEach(computers, cpt=> { cpt.Logs = cpt.GetRawLogs().ToList(); yield return cpt // KO, don't work }); } EDIT It seems not to be possible... but I use something like this : public IEnumerable<IComputer> FillLogs(IEnumerable

Parallel.For not to use my main thread

自古美人都是妖i 提交于 2019-11-28 12:49:53
In my application I want my main thread to be not used by anything else. I have to do some parallel processing that I would like to be done by different threads. For that I am using Parallel.For as follows static void SomeMethod() { Console.WriteLine(string.Format("Main Thread ID before parallel loop ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId)); Parallel.For(0, 10, i => { Console.WriteLine(string.Format("Output ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId)); }); Thread.Sleep(100); Console.WriteLine(string.Format("Main Thread ID after parallel

Parallel.ForEach() vs. foreach(IEnumerable<T>.AsParallel())

▼魔方 西西 提交于 2019-11-28 02:59:58
Erg, I'm trying to find these two methods in the BCL using Reflector, but can't locate them. What's the difference between these two snippets? A: IEnumerable<string> items = ... Parallel.ForEach(items, item => { ... }); B: IEnumerable<string> items = ... foreach (var item in items.AsParallel()) { ... } Are there different consequences of using one over the other? (Assume that whatever I'm doing in the bracketed bodies of both examples is thread safe.) They do something quite different. The first one takes the anonymous delegate, and runs multiple threads on this code in parallel for all the

How many threads Parallel.For(Foreach) will create? Default MaxDegreeOfParallelism?

我们两清 提交于 2019-11-27 22:56:55
I want to know, how many threads will be used when I run Parallel.For/ForEach loop. I found, that it can be changed by MaxDegreeOfParallelism option. MaxDegreeOfParallelism help on MSDN says ( link ): By default, For and ForEach will utilize however many threads the underlying scheduler provides, so changing MaxDegreeOfParallelism from the default only limits how many concurrent tasks will be used. But I don't know how many threads underlying scheduler provides. How can I find out that? I could test it with loop with 9999999 runs, however this test will show me number, but not the rule that