parallel.foreach

WPF : Progress Bar update in Parallel.Foreach

夙愿已清 提交于 2020-01-26 04:21:05
问题 I am trying to update the progress bar while running a Parallel.Foreach , but during execution nothing happens. Progressbar gets updated only when the For loop end. How can I make this code work? XAML <StackPanel> <Grid x:Name="LoadProgressGrid" Height="100" Visibility="Visible"> <ProgressBar x:Name="LoadProgress" Maximum="100" Minimum="1" /> <TextBlock Margin="0,0,0,-5" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16"><Run Text="Import Progress"/></TextBlock> </Grid>

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

三世轮回 提交于 2020-01-19 05:40:29
问题 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

Passing async method into Parallel.ForEach

若如初见. 提交于 2020-01-10 04:37:08
问题 I was reading this post about Parallel.ForEach where it was stated that "Parallel.ForEach is not compatible with passing in a async method." So, to check I write this code: static async Task Main(string[] args) { var results = new ConcurrentDictionary<string, int>(); Parallel.ForEach(Enumerable.Range(0, 100), async index => { var res = await DoAsyncJob(index); results.TryAdd(index.ToString(), res); }); Console.ReadLine(); } static async Task<int> DoAsyncJob(int i) { Thread.Sleep(100); return

How do I iterate through a dynamically valued dictionary in parallel?

坚强是说给别人听的谎言 提交于 2020-01-05 04:54:08
问题 I am trying to spawn threads in a for each loop using a copy of the value in a dict. My initial understanding was that the foreach would create a new scope, and led to: Dictionary<string, string> Dict = new Dictionary<string, string>() { { "sr1", "1" }, { "sr2", "2" } }; foreach (KeyValuePair<string, string> record in Dict) { new System.Threading.Timer(_ => { Console.WriteLine(record.Value); }, null, TimeSpan.Zero, new TimeSpan(0, 0, 5)); } which writes 1 2 2 2 instead of (expected): 1 2 1 2

Why does Parallel.ForEach change the culture of its threads?

随声附和 提交于 2020-01-03 17:09:15
问题 Today I came across a strange phenomenon I can't really explain. There's a webpage with a number of rows in a gridview, which need to be saved to the database and to an XML file one by one. I ended up using a Parallel.ForEach , as there is no relation between the rows, so they can be executed independently. The code is basically this: Parallel.ForEach(gvWithData.Rows.Cast<GridViewRow>(), row => { if (row.RowType == DataControlRowType.DataRow) { // do some logic and stuff... var type = new

What is the performance differences between using parallel.foreach and task inside foreach loop?

风格不统一 提交于 2020-01-02 05:54:14
问题 I would like to know what is the best way or are there any documents/articles that can help me to identify what is the differences of using Parallel.foreach and Task within a normal for each loop, like the following: case 1 - Parallel.foreach: Parallel.foreach { // Do SOmething thread safe: parsing an xml and then save // into a DB Server thry respoitory approach } case 2 - Task within foreach: foreach { Task t1 = Task.factory.startNew(()=> { //Do the same thing as case 1 that is thread safe

Parallel.ForEach() changes Impersonation Context

为君一笑 提交于 2020-01-01 03:01:51
问题 Today we deployed our newly created ASP.NET application to the server and soon we realized there was a strange security-related issue which was causing the application to crash. This is an internal application and we use Impersonation to manage how the users access resources. The application however, throws an "Access Denied" exception when the user attemps to access a folder over which they have full control. The exception was in fact an AggregateException and was being thrown in a method

Parallel.Foreach loop, inconsistent behavior with explicit throw statement

那年仲夏 提交于 2019-12-31 02:43:10
问题 Created a simple program using Linqpad, where I am throwing an exception explicitly in the Parallel Foreach loop, which ideally shall be caught in the caller as Aggregate Exception , but when I explicitly throw the exception, it sometimes skip out few exceptions on random basis. I am not able to understand the behavior, anyone who can explain: void Main() { try { var intList = new List<int> {1,2,3,4,5,6}; Parallel.ForEach(intList, i => Test1(i)); } catch (AggregateException aggregateException

Parallelize, minimize time Reading number of sheets from Excel file

倾然丶 夕夏残阳落幕 提交于 2019-12-25 07:58:30
问题 I have Excel file has 4 sheets, all sheets have huge number of records around 144564 record. In my code, fill_data method will call 4 methods, each method read one sheet. the normal time it takes now is 7.02 minutes. fill_data () { Excel.Application ExcelApplication; Excel.Workbooks ExcelWorkbooks; Excel.Workbook ExcelWorkbook1; ExcelApplication = new Excel.Application(); /// ApplicationClass() ExcelWorkbooks = ExcelApplication.Workbooks; try { ExcelWorkbook1 = ExcelApplication.Workbooks.Open

R: get list and environment of all variables and functions within a given function (for parallel processing)

喜夏-厌秋 提交于 2019-12-24 08:03:11
问题 I am using foreach for parallel processing, which requires manual passing of functions via a list to the environments of addressed cores. I want to automate this process and cover all use cases. Easy for simple functions which use only enclosed variables. Complications however as soon as functions which are to be parallel processed are using arguments and variables that are defined in another environment. Consider the following case: global.variable <- 3 global.function <-function(j){ res <-