task-parallel-library

Share data between threads

落爺英雄遲暮 提交于 2020-01-05 07:45:10
问题 I'm trying to implement an algorithm that should run in parallel using threads or tasks. The difficulty is that I want the threads/tasks to share their best results from time to time with all other threads. The basic idea is this: //Accessible from each thread IProducerConsumerCollection<MyObject> _bestObjects; //Executed in each thread DoSomeWork(int n){ MyObject localObject; for(var i = 0; i < n; i++){ //Do some calculations and store results in localObject if((i/n)%0.5 == 0) { //store

Read file faster by multiple readers

落爺英雄遲暮 提交于 2020-01-04 14:32:13
问题 So i have a large file which has ~2 million lines. The file reading is a bottleneck in my code. Any suggested ways and expert opinion to read the file faster is welcome. Order of reading lines from that text file is unimportant. All lines are pipe '|' separated fixed length records. What i tried? I started parallel StreamReader s and made sure that resource is locked properly but this approach failed as i now had multiple threads fighting to get hold of the single StreamReader and wasting

Httpclient tpl parallel multiple request fastest way

耗尽温柔 提交于 2020-01-04 14:19:12
问题 I want to download webPages content of url list (10 000 urls). Is httpCLient the fastest and cleanest way (instead httpwebrequest, or webclient)? If I want to be fast, Is TPL the best way ? I'm looking for something like, but really fast and clean (10 000 request) ? public List<string> GetContentListOfUrlList(List<Uri> uriList, int maxSimultaneousRequest) { //requesting url by the fastest way } I hope is better like this ;) EDIT 2 : According to noseratio other post Is the best solution ?

Real purpose of providing TaskScheduler option through ParallelOptions parameter in Parallel class APIs

坚强是说给别人听的谎言 提交于 2020-01-04 14:00:56
问题 I have a windows C# WinForms application. I'm confused by the way the default task scheduler is behaving for parallel class in task parallel library. When I call Parallel.For inside ParallelForEach method I was expecting the numbers to get printed in jumbled fashion in output window through Console.WriteLine statement inside "Method1" method. Since I'm not passing any ParallelOptions to configure the scheduler for Parallel class it should be using default task scheduler but numbers are

Does tasks.ToList() create a list containing newly copied tasks or the list refers to the same tasks?

ぐ巨炮叔叔 提交于 2020-01-04 13:46:20
问题 Assume we have an array of tasks (called 'tasks') and then convert it to a list (called 'temp') by saying: var temp = tasks.ToList(); What happens to those running tasks to which the array elements point? Do we have two sets of tasks running separately (one in the 'tasks' and the other in 'temp')? Or they point to the same tasks? The following code (taken from the book Exam Ref 70-483 ) is relevant to what I'm saying (the last three lines): Task<int>[] tasks = new Task<int>[3]; tasks[0] =

Parallel.For performance

╄→尐↘猪︶ㄣ 提交于 2020-01-04 13:42:18
问题 This code is from Microsoft article http://msdn.microsoft.com/en-us/library/dd460703.aspx, with small changes: const int size = 10000000; int[] nums = new int[size]; Parallel.For(0, size, i => {nums[i] = 1;}); long total = 0; Parallel.For<long>( 0, size, () => 0, (j, loop, subtotal) => { return subtotal + nums[j]; }, (x) => Interlocked.Add(ref total, x) ); if (total != size) { Console.WriteLine("Error"); } Non-parallel loop version is: for (int i = 0; i < size; ++i) { total += nums[i]; } When

Parallel.For performance

别说谁变了你拦得住时间么 提交于 2020-01-04 13:41:32
问题 This code is from Microsoft article http://msdn.microsoft.com/en-us/library/dd460703.aspx, with small changes: const int size = 10000000; int[] nums = new int[size]; Parallel.For(0, size, i => {nums[i] = 1;}); long total = 0; Parallel.For<long>( 0, size, () => 0, (j, loop, subtotal) => { return subtotal + nums[j]; }, (x) => Interlocked.Add(ref total, x) ); if (total != size) { Console.WriteLine("Error"); } Non-parallel loop version is: for (int i = 0; i < size; ++i) { total += nums[i]; } When

Parallelizing a for loop with stepping in .net 4.5

試著忘記壹切 提交于 2020-01-04 04:12:21
问题 I have a function that looks something like the code below. Its purpose is to take triangle facets one at a time from an array of points where each three points is one facet, and tessellate them, replacing the facet with a list of smaller facets whose side lengths don't exceed nodeSize. Naturally, this function is time consuming for any realistic facet mesh. I'd like to refactor it to use some coarse parallelization. However, Parallel.For doesn't appear to have a way to step through indices

Is it safe to use ContinueWith as a “finally” operation?

寵の児 提交于 2020-01-03 10:22:14
问题 Consider a piece of code like: private Task<string> Download() { var wc = new WebClient(); Task<string> backgroundDownload = wc.DownloadStringTaskAsync(this.Uri); // Make sure the WebClient is disposed no matter what. backgroundDownload.ContinueWith((downloadTask) => { wc.Dispose(); }); return backgroundDownload; } Can I be certain that the WebClient.Dispose() call occurs and that any exception produced is rethrown to the caller as if there was no call to ContinueWith ? Can clients observe

Throwing exceptions from ContinueWith

北战南征 提交于 2020-01-03 09:05:14
问题 I am trying to wrap the exceptions that can be thrown by an async task using ContinueWith() . If I just throw from the continuation action things seem to work, but my debugger claims the exception is unhandled. Am I doing something wrong or is this a Visual Studio problem? Is there a cleaner way to do this, or a way to work around my debugger stopping on what is ultimately a handled exception? The test below passes and prints "caught wrapped exception as expected", but when I debug it the