task-parallel-library

Nested Parallel.ForEach Loops on the same list?

I need to parallelize a method that does an exhaustive pairwise comparison on elements in a list. The serial implementation is straight-forward: foreach (var element1 in list) foreach (var element2 in list) foo(element1, element2); In this case, foo won't alter the state of element1 or element2. I know it's not safe to sim...

TaskScheduler.UnobservedTaskException event handler never being triggered

I'm reading through a book about the C# Task Parallel Library and have the following example but the TaskScheduler.UnobservedTaskException handler is never being triggered. Can anyone give me any clues as to why? TaskScheduler.UnobservedTaskException += ( object sender, UnobservedTaskExceptionEventArgs eventArgs ) => { ...

Task Parallel Library. Nested task do not start

Hi I'm working with .NET new TPL library and faced with some strange behavior for me I cannot explain. For some reason nested task is not started in my case. I've simplified the solution I have to the following: bool flag = false; for (int i = 0; i < 5; i++) { Task.Factory.StartNew(() => ...

Why does Parallel.Invoke fail to complete all the actions?

I worked on an example in the text book about parallel programming in C#. The book suggests Parallel.Invoke can replace the creation, invocation and waiting of tasks. However, I tried and found that if I use Parallel.Invoke, the tasks wouldn't finish before returning a value. But in theory, Parallel.Invoke should always wait. The code: ...

Tracking progress of a multi-step Task

Hello, I am working on a simple server that exposes webservices to clients. Some of the requests may take a long time to complete, and are logically broken into multiple steps. For such requests, it is required to report progress during execution. In addition, a new request may be initiated before a previous one completes, and it is req...

Does the Task Parallel Library (or PLINQ) take other processes into account?

In particular, I'm looking at using TPL to start (and wait for) external processes. Does the TPL look at total machine load (both CPU and I/O) before deciding to start another task (hence -- in my case -- another external process)? For example: I've got about 100 media files that need to be encoded or transcoded (e.g. from WAV to FLAC ...

Is it possible to use the Task Parallel Library (TPL) in C# 2.0?

Currently stuck in C# 2, it would still be nice to use the parallel goodness of the TPL... is this possible? ...

Parallelize the content retrieval from database, search and store as HTML

I have a database table having HTML content stored as binary serialized blob. I need to retrieve content one by one, look for certain keywords in the content (and report the matches found) and also save the content to the disk as HTML files. Can I parallize this using Parallel.ForEach? Is this a good Idea or there is a better one. Thank...

Task parallel library replacement for BackgroundWorker?

Does the task parallel library have anything that would be considered a replacement or improvement over the BackgroundWorker class? I have a WinForms application with a wizard-style UI, and it does some long-running tasks. I want to be able to have a responsive UI with the standard progress bar and ability to cancel the operation. I've...

Is there an interface wrapper around the Task Parallel Library so that I can swap it out for unit testing?

I asked this question a while ago. I now know it is a Bad Idea and that encapsulating the scheduling and running of tasks should be abstracted so that one can pass in a synchronous scheduler from the unit tests. I've currently got code that uses the Task Parallel Library (TPL) and I'd like to inject something like ITaskScheduler into ...

Is there a way to throttle the threads used by Task Parallel Library?

I'm using the TPL but I'm finding it tricky unit testing code that uses it. I'm trying to not introduce a wrapper over it as I feel it could introduce issues. I understand that you can set processor affinity in the TPL, but what'd be really nice is to set a thread maximum (probably per app-domain). Therefore, when setting the thread m...

Not able to see Parallel Tasks window in Visual studio 2010 RTM

The parallel task window should be under Debug > Windows in Visual Studio. However, I see only "Breakpoints" there. Am I missing anything? ...

Implementing 'only one of' and 'not in parallel' semantics with the Task parallel library

What would be the best approach for implementing Tasks with a KEY that operate as follows:- Option 1) Only one of this key is ever pending. Can be used, for example, from ASP.NET MVC to queue up a single render for a thumbnail image no matter how many times the image Url is hit. Only one runs, all other requests wait for that one to c...

Is it possible to change parallelOptions.MaxDegreeOfParallelism during execution of a Parallel.ForEach

I am running a multi-threaded loop: protected ParallelOptions parallelOptions = new ParallelOptions(); parallelOptions.MaxDegreeOfParallelism = 2; Parallel.ForEach(items, parallelOptions, item => { // Loop code here }); I want to change the parallelOptions.MaxDegreeOfParallelism during the execution of the parallel loop to reduce or ...

Cancellation token in Task constructor: why?

Certain System.Threading.Tasks.Task constructors take a CancellationToken as a parameter: CancellationTokenSource source = new CancellationTokenSource(); Task t = new Task (/* method */, source.Token); What baffles me about this is that there is no way from inside the method body to actually get at the token passed in (e.g., nothing l...

Parallel.Invoke - Exception handling

My code runs 4 function to fill in information (Using Invoke) to a class such as: class Person { int Age; string name; long ID; bool isVegeterian public static Person GetPerson(int LocalID) { Person person; Parallel.Invoke(() => {GetAgeFromWebServiceX(person)}, () => {Get...

Is it considered acceptable to not call Dispose() on a TPL Task object?

I want to trigger a task to run on a background thread. I don't want to wait on the tasks completion. In .net 3.5 I would have done this: ThreadPool.QueueUserWorkItem(d => { DoSomething(); }); In .net 4 the TPL is the suggested way. The common pattern I have seen recommended is: Task.Factory.StartNew(() => { DoSomething(); }); How...

Backgroundworker and TPL's Task have the same ManagedThreadID?

I have a Backgroundworker whose purpose is to run jobs sequentially in the background. Now one job is implemented in multithreading way. That mean, the Backgroundworker will create several threads. I use Task Parallel Library so I use the Task.Factory.StartNew to create multiple Tasks. After the tasks are run, the Backgroundworker waits...

Using .Net 4.0 new features for parallel tasks

I've previously asked a question about designing a service that receives video files, sends them to an encoding service, waits for the encoding to be completed, and then downloads the files. I started writing the code for that and one of my workmates suggested I use .Net 4.0 new features, instead of writing it using BackgroundWorker. I'...

Indexing Lucene with Parallel Extensions (TPL)

I'd like to speed-up the indexing of 10GB of data into a Lucene index. Would TPL be a good way to do this? Would I need to divided the data up into chunks and then have each thread start indexing chunks? To keep the UI responsive would BackgroundWorker be the best approach, or Task, or something else? Does SOLR already do something l...