Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Parallel Task.ContinueWith order of execution

Question

This is probably something simple I've missed.

Let's say I do (in loveley VB):

Dim t1 As New Task(Sub() Debug.WriteLine("t1"))
Dim t2 As Task = t1.ContinueWith(Sub() Debug.WriteLine("t2"))
Dim t3 As Task = t1.ContinueWith(Sub() Debug.WriteLine("t3"))
t1.Start()

Notice t1.ContinueWith is used twice. What is the preferred order of execution for these tasks? For me, it's either random or wrong.

Why?

The reason I want to do this...

  1. Create a task grabbing and returning data
  2. Followed by a task which "sorts out" the UI on TaskScheduler.FromCurrentSynchronizationContext() (UI thread)
  3. Follow the original data grabbing task by converting the data into a long report (takes ages)
  4. Back on the UI thread, assign that report to a previewing control
  5. Cleanup (get rid of my progress animation etc)

This is made more complicated by the fact that step 4 is optional.

In the midst of all this, my tasks are bending backwards to set member properties so the UI and tasks can play nice. Perhaps I should ditch the Task result entirely and just stick to Synclocking my member variables. They are all only assigned once afterall.

Thanks, Tom

like image 747
Tom Avatar asked Nov 19 '10 15:11

Tom


People also ask

Do tasks run in parallel?

Concurrent tasks progress at the same time in the worker system but they don't progress simultaneously. Parallel tasks are executed by different workers at the same time. Concurrency refers to how a worker system handles multiple tasks while parallelism refers to how a worker system handles a single task.

What does calling Task ContinueWith () do?

ContinueWith(Action<Task,Object>, Object, TaskScheduler)Creates a continuation that receives caller-supplied state information and executes asynchronously when the target Task completes.

What is task parallelism in C#?

The Task Parallel Library (TPL) is based on the concept of a task, which represents an asynchronous operation. In some ways, a task resembles a thread or ThreadPool work item but at a higher level of abstraction. The term task parallelism refers to one or more independent tasks running concurrently.

What is ContinueWith C#?

The ContinueWith function is a method available on the task that allows executing code after the task has finished execution. In simple words it allows continuation. Things to note here is that ContinueWith also returns one Task. That means you can attach ContinueWith one task returned by this method.


1 Answers

The tasks are executed in a LIFO ordering for better memory locality. This can be changed of course if you use a differnt scheduler or if MS decides to "fix" the original one. I do not think that you should rely on this behaviour. Instead you could Unwrap your task to continue working on the previous task when it has completed. Otherwise you are waiting on the wrong thing. More infos can be found here: http://msdn.microsoft.com/en-us/library/ee795275.aspx

like image 166
Alois Kraus Avatar answered Sep 28 '22 22:09

Alois Kraus