parallel.foreach

Parallel.ForEach using Thread.Sleep equivalent

♀尐吖头ヾ 提交于 2019-12-19 17:41:33
问题 So here's the situation: I need to make a call to a web site that starts a search. This search continues for an unknown amount of time, and the only way I know if the search has finished is by periodically querying the website to see if there's a "Download Data" link somewhere on it (it uses some strange ajax call on a javascript timer to check the backend and update the page, I think). So here's the trick: I have hundreds of items I need to search for, one at a time. So I have some code that

Parallel.For not to use my main thread

心已入冬 提交于 2019-12-17 20:38:49
问题 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

Garbage Collection and Parallel.ForEach Issue After VS2015 Upgrade

喜欢而已 提交于 2019-12-17 10:52:12
问题 I have some code to process several million data rows in my own R-like C# DataFrame class. There's a number of Parallel.ForEach calls for iterating over the data rows in parallel. This code has been running for over a year using VS2013 and .NET 4.5 without issues. I have two dev machines (A and B) and recently upgraded machine A to VS2015. I started noticing a strange intermittent freeze in my code about half the time. Letting it run for a long time, it turns out that the code does eventually

Async/await and parallel in C# [closed]

青春壹個敷衍的年華 提交于 2019-12-17 06:34:15
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . When should I use async/await and when should I use parallel.foreach in C#? Are parallel and async/await serve the same purpose? What

What does MaxDegreeOfParallelism do?

非 Y 不嫁゛ 提交于 2019-12-17 05:09:49
问题 I am using Parallel.ForEach and I am doing some database updates, now without setting MaxDegreeOfParallelism , a dual core processor machine results in sql client timeouts, where else quad core processor machine somehow does not timeout. Now I have no control over what kind of processor cores are available where my code runs, but is there some settings I can change with MaxDegreeOfParallelism that will probably run less operations simultaneously and not result in timeouts? I can increase

Parallel.ForEach Slower than ForEach

好久不见. 提交于 2019-12-17 03:58:30
问题 Here is the code: using (var context = new AventureWorksDataContext()) { IEnumerable<Customer> _customerQuery = from c in context.Customers where c.FirstName.StartsWith("A") select c; var watch = new Stopwatch(); watch.Start(); var result = Parallel.ForEach(_customerQuery, c => Console.WriteLine(c.FirstName)); watch.Stop(); Debug.WriteLine(watch.ElapsedMilliseconds); watch = new Stopwatch(); watch.Start(); foreach (var customer in _customerQuery) { Console.WriteLine(customer.FirstName); }

Parallel.ForEach vs Task.Run and Task.WhenAll

故事扮演 提交于 2019-12-17 02:53:46
问题 What are the differences between using Parallel.ForEach or Task.Run() to start a set of tasks asynchronously? Version 1: List<string> strings = new List<string> { "s1", "s2", "s3" }; Parallel.ForEach(strings, s => { DoSomething(s); }); Version 2: List<string> strings = new List<string> { "s1", "s2", "s3" }; List<Task> Tasks = new List<Task>(); foreach (var s in strings) { Tasks.Add(Task.Run(() => DoSomething(s))); } await Task.WhenAll(Tasks); 回答1: In this case, the second method will

How to parallelize an azure worker role?

本小妞迷上赌 提交于 2019-12-14 04:25:56
问题 I have got a Worker Role running in azure. This worker processes a queue in which there are a large number of integers. For each integer I have to do processings quite long (from 1 second to 10 minutes according to the integer). As this is quite time consuming, I would like to do these processings in parallel. Unfortunately, my parallelization seems to not be efficient when I test with a queue of 400 integers. Here is my implementation : public class WorkerRole : RoleEntryPoint { private

Iterator in a Parallel.ForEach is run on multiple threads

依然范特西╮ 提交于 2019-12-13 16:24:49
问题 I have some code that I run within a Parallel.ForEach loop. The code in the loop is thread safe, but the iterator that I'm using (a custom method with yield return ) is not. It appears that the iterator is being run on multiple threads which could potentially cause issues. Background to the problem The iterator contains calls to NHibernate (althought that's entirely incidental as you'll see later) and as I was having issues parallelising the code I looked in NHibernate Profiler to see what if

Parallel.ForEach slower than normal foreach

半城伤御伤魂 提交于 2019-12-12 16:41:15
问题 I'm playing around with the Parallel.ForEach in a C# console application, but can't seem to get it right. I'm creating an array with random numbers and i have a sequential foreach and a Parallel.ForEach that finds the largest value in the array. With approximately the same code in c++ i started to see a tradeoff to using several threads at 3M values in the array. But the Parallel.ForEach is twice as slow even at 100M values. What am i doing wrong? class Program { static void Main(string[]