Look, we all copy from StackOverflow. Sometimes blindly since whatever snippet "just works" and we move on. In the same lane, here's some easy stuff without being technically in depth - almost like dipping toes in just to get a little experience. Though as always, there's way more to it like breaking, cancelling, chaining, but let's keep it simple for now.

Parallel Loops

My favourite part of the TPL. You'll see me using this in Windows services or console applications a lot for data crunching, image processing or anything else that needs a speed up. I wanna see those cores running hot.

Parallel.ForEach(customerList, (customer) =>
{
	// code here
});

Or if you're looking for a regular for

Parallel.For(0, customerList.Count(), i =>
{
	// code here
});

Running Multiple Tasks and Waiting for Them All to Return

Maybe we want to load up a lot of data from various sources and we can't continue without it. We can fire off all the calls in parallel and only continue once they've all returned. The methods in the array all return Task of some sort. It's normally a bad decision (except for events) to return void with a Task.  

var taskList = new[]
{
    GetApiData(),
    GetDatabaseData(),
    ReadDataFile()
};

Task.WaitAll(taskList);

And that's it, a super easy way into the TPL. Even if you sensibly replace one of your loops, you made it - toes are wet and you're ready to move to more advanced interesting topics like:

  • Async/Await
  • TPL exception handling (a little different from regular exception handling)
  • Cancelling
  • PLINQ

Good Luck!