Task.Run(() => - order of processing ......multithreading in foreach loop?

Hi, I have a question about the order of the code is processed in multi threaded envrioment

I have a bunch of cars with tracking data
I need to look at each cars tracking record (ordered by datetime) and do some stuff (it HAS to be processed in datetime order - one at a time)

This was way to slow, so now i've made the app multithreaded,
I've essentially got
GROUPS - (each item in group is 1 car)
POSITIONS - (all the cars positions data in right order)

--Car 1
-----All Car 1 Positions

--Car 2
-----All Car 2 Positions


In my code I have this working, but i'm not sure if they will be processed as I want
i.e. I need car 1 and car 2 etc to be processed at same time of different threads, but their POSITION data to be processed 1 at a time in right order
consider the following code

foreach (var car in GroupOfCarsAndPositions.ToList())

                    Task.Run(() =>
                       //each car should be processed at the same time (multithreaded)

                        foreach (var position in car.PositionsList.ToList())
                              //each position  Item in this group should be processed one at a time

Open in new window

my question is, will the first foreach be processed multithreaded? (i.e. each car be processed at same time?)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

will the first foreach be processed multithreaded?
no, the first foreach will run in the main thread and create tasks for each car sequentially in the loop.  then, each task will run asynchronously (that is what you expected) and perform the second loop. but, after the first foreach you may have to wait for the tasks to finish in order to evaluate the results. alternatively, your main thread may run a gui and process the results of the tasks within the gui.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
Simplest if you need them to be processed in order then you must do the processing in the same thread.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.