Reputation: 2995
I have inherited a Winforms application in .NET 4 (I cannot change this). The application runs several tasks in parallel using TPL. First, a Task is created and from that task many more tasks are generated which do complex calculations. The main Task is waiting with WaitAll(tasks)
.
The problem is that these little calculation tasks report progress to the main form, in each progress report the application calls BeginInvoke()
to update the progress bar but it is updated only after all the tasks are finished. I believe the problem is that the main Task runs on the UI thread and by calling WaitAll()
it blocks that thread so the BeginInvoke()
calls are piling up on the event loop.
What is the correct approach in this case?
Reading other SO answers it seems that I cannot force a Task to run on a ThreadPool thread so I am inclined to replace the main Task with a BackgroundWorker.
Upvotes: 1
Views: 815
Reputation: 286
setting the option TaskCreationOptions.LongRunning on your initial task will cause it to run on a separate thread (not a thread pool thread)
Upvotes: 2