Write Asynchronous C# Code With Tasks and PLINQ

A comprehensive course teaching you how to write asynchronous C# code with the Task Parallel Library and PLINQ.
4.3 (45 ratings) Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
960 students enrolled
$19
$50
62% off
Take This Course
  • Lectures 26
  • Length 3 hours
  • Skill Level All Levels
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works

Discover

Find online courses made by experts from around the world.

Learn

Take your courses with you and learn anywhere, anytime.

Master

Learn and practice real-world skills and achieve your goals.

About This Course

Published 10/2015 English

Course Description

BEST COURSE CHOICE for beginners and intermediate C# developers who want to learn about the Task Parallel Library and Parallel LINQ, and take their asynchronous programming skills to the next level.

Do you know how to write robust multi-threaded C# code that does not crash?

Yeah, I thought I did too. Ten years ago I built code for an RFID-enabled supermarket shelf. But my code was very unstable, and the shelf required many reboots to run successfully. Needless to say, the client was not amused.

So what was the problem? This was years before the Task Parallel Library and Parallel LINQ were released. So I had to do everything manually: locking shared fields, synchronising threads, you name it. And of course I had overlooked a few bits of code that were not thread-safe.

Don't let this happen to you!

Today we have beautiful libraries for writing asynchronous C#, and there is simply no excuse for writing bad code. In this course I will teach you how to write rock-solid code using Tasks and Parallel LINQ that works perfectly on your first try.

I wrote a multi-threaded conversion utility a few months ago that successfully migrated 100,000 documents from SharePoint 2010 to SharePoint 2013. The program worked flawlessly the first time because I used PLINQ.

Sound good?

Writing multi-threaded code by hand is hard. I’ll prove it to you by showing you some code that exchanges data between two threads. You will be surprised how difficult it is to do this reliably.

But then I’ll show you how trivially easy it is to write asynchronous C# code using the Task Parallel Library and Parallel LINQ. These amazing frameworks allow anyone to write robust multi-threaded code that can take a beating.

By the end of the course you will be fluent in both the Tasks Parallel Library and Parallel LINQ.

Why should you take this course?

You should take this course if you are a beginner or intermediate C# developer and want to take your skills to the next level. Working with Tasks and Parallel LINQ might sound complicated, but all of my lectures are very easy to follow, and I explain all topics with clear code and many instructive diagrams. You'll have no trouble following along.

Or maybe you're working on a critical asynchronous section of C# code in a lage project, and need to make sure your code scales reliably over multiple CPU cores? The tips and tricks in this course will help you immensely.

Or maybe you're preparing for a C# related job interview? This course will give you an excellent foundation to answer any asynchronous programming questions they might throw at you.

30 day money-back guarantee

This course comes with an unconditional, Udemy backed, 30-day money-back guarantee. If you are dissatisfied with the course for any reason, simply request a refund and get your full purchase amount back, no questions asked.

Act now

This course today contains over 3 hours of video content and I am adding new material every month. Enrol now to benefit from the current price and get free lifelong access to all future materials.

Enrol now!

What are the requirements?

  • You should have a Windows, Mac or Linux laptop with Microsoft Visual Studio, Xamarin Studio or Monodevelop installed
  • You should be familiar with the C# language
  • That's all!

What am I going to get from this course?

  • Learn how to write robust asynchronous C# code from a certified Microsoft trainer from your own desk.
  • Many hours of video content teaching you about threads, race conditions, synchronisation, tasks, and parallel LINQ!
  • Suitable for beginner and intermediate programmers and ideal for users who learn faster when shown.
  • Learn how to use the Task class
  • Join tasks together into complex networks to implement MapReduce operations
  • Learn how to parallelise any LINQ query
  • Understand the differences between the Task Parallel Library and Parallel LINQ

What is the target audience?

  • This course is for beginner and intermediate C# programmers who want to learn about the Task Parallel Library and Parallel LINQ
  • Ideal for students who are about to take a job interview, and need to prepare for asynchronous code questions
  • Are you writing a section of asynchronous code in a large C# project, and need to be sure it will scale in production? Then this course is for you!

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.

Curriculum

Section 1: Introduction
07:02

In this lecture I explain how this course is organised and I describe each of the upcoming sections in detail.

03:50

In this lecture I will tell a bit more about myself, my career, and my motivation to become an online trainer.

06:17

In this lecture we're going to look at the Task Parallel Library and PLINQ, and what they can do for you.

03:19

Many lectures in this course contain source code examples. Feel free to download the code and follow along. And here's the good news: it doesn't matter if you have a Window, Mac or Linux computer. The code will run on all three operating systems.

In this lecture I demonstrate how my solutions and projects run on all operating systems. I will show you how to build and run the source code on a Mac, on Linux and in Visual Studio running on Windows 8.

At the end of this lecture you will have learned that .NET code is portable and can run on at least five different operating systems.

Section 2: Multi-Threaded Code Fundamentals
01:53

Welcome to the Multi-Threaded Code Fundamentals section. I will give a quick introduction on how the section is organized before we get started.

08:52

In this lecture I will teach you how to start new threads using the System.Thread class: the workhorse of multi-threaded programming in C#.

I will also show you how you can give a descriptive name to a thread, to aid in debugging.

Finally, you will learn that there are two classes of threads: foreground- and background threads. I will show you the difference in behaviour between these two classes of threads.

03:43

In this lecture I am going to show you the most common multi-threading programming problem: a race condition.

A race condition happens when 2 or more threads are trying to access and modify the same variable. I will demonstrate a race condition with a very simple program, with 2 threads accessing a shared integer class member.

07:55

In this lecture I'll demonstrate how a special technique called 'thread locking' can resolve race conditions. I will show you several short examples of code prone to race conditions, and then I'll add thread locking to the code to fix the problem.

At the end of this lecture you will know exactly what thread locking is, how it resolves a race condition, and when you should implement it yourself.

10:54

In this lecture we are going to take a closer look at the lock statement in C#. You will learn that "lock" is in fact syntactic sugar for a pair of Monitor.Enter and Monitor.Exit calls. I will demonstrate several example programs using either the compact "lock" syntax, or the more verbose code that uses the Monitor class.

You will learn all the essentials of thread locking, including what code to lock, which synchronisation object to use, and what the advantages are of calling the Monitor class directly.

By the end of the lecture you will be proficient in thread locking, and you will be able to set up critical sections in your code with ease.

14:30

In this lecture I am going to take a look at thread synchronisation. The need for thread synchronisation arises when two or more threads need to exchange data in a controlled manner. I will show you a simple example program that attempts to exchange data between threads without any synchronisation, and you will see how the data transfer completely fails.

Next we will cover the workhorse of thread synchronisation: the AutoResetEvent. I will show you how you can line up two threads with a single AutoResetEvent variable, to ensure that you'll never lose any data. Then I'll show you how the single remaining race condition can be resolved by adding a second AutoResetEvent.

By the end of the lecture you will have a deep understanding of thread synchronisation: what it is, when you need it, and how you can implement it yourself.

04:16

Congratulations on finishing this section. This is a recap of what we have learned.

Multi-Threaded Code Fundamentals
5 questions
Section 3: Working With Tasks
01:49

Welcome to the Working With Tasks section. I will give a quick introduction on how the section is organized before we get started.

11:57

In the previous section you learned how to line up two threads with AutoResetEvents in order for them to exchange data. A reliable communication channel between two threads required two AutoResetEvents and a locked shared variable.

In this lecture I will show you how to implement the same channel using the Task class. You will learn that using tasks is much simpler than building the channel by hand.

12:19

In this lecture we're going to take a closer look at tasks. We have already encountered generic tasks that can return a value to another thread. This lecture will introduce non-generic tasks that run asynchronously in the background without returning any data. I'll show you how to start a non-generic task and wait for it to complete.

I'll also demonstrate how the .NET runtime uses a thread pool to execute tasks, and how you can fine-tune how threads are assigned to tasks. Finally we'll discover what happens when a task throws an exception.

06:54

In this lecture I will show you how to start a task with initialisation data. You will also learn how to cancel a running task by using a cancellation token.

14:35

The nice thing about the Task Parallel Library is that you can connect together thousands of tasks into complex patterns. In this lecture you will learn how you can create hierarchies of parent and child tasks.

I will show you how to write a simple program that reverses the characters in each word using parent and child tasks.

12:37

The Task Parallel Library lets you connect together thousands of tasks into complex patterns. In this lecture you will learn how you can create sequences of tasks with continuations. By combining task hierarchies with continuations you can implement any MapReduce problem in C#.

I will revisit the word reversing program from the previous lecture. I will use continuations and child tasks to rewrite the code as a MapReduce solution.

08:10

There are three asynchronous libraries that you can use in C#: the Task Parallel Library, the Parallel class, and the Parallel LINQ library. Sometimes it can be difficult to choose between them.

In this lecture we are going to take a closer look at the pros and cons of the Task Parallel Library. By the end of the lecture you will know exactly when to use this Library.

05:10

Congratulations on finishing this section. This is a recap of what we have learned.

Working With Tasks
6 questions
Section 4: Using Parallel LINQ
01:57
Welcome to the Parallel LINQ section. I will give a quick introduction on how the section is organized before we get started.


07:21

There are three asynchronous libraries that you can use in C#: the Task Parallel Library, the Parallel class, and the Parallel LINQ library. Sometimes it can be difficult to choose between them.

In this lecture we are going to take a closer look at the pros and cons of the Parallel LINQ library. By the end of the lecture you will know when to use this library.

12:00

The Parallel LINQ library is ideal for implementing MapReduce operations. In the previous section we learned that my word reversal program can be expressed as a MapReduce problem and implemented with a combination of parent / child tasks and task continuations.

In this lecture I will revisit the program and reimplement it entirely in only a few lines of PLINQ code. By the end of the lecture you will have learned how to parallelise a LINQ query and fine-tune it for best performance.

06:22

In the previous lecture I showed you how to reimplement the word reversal program in PLINQ. But there was something wrong with the final result, when I used WithExecutionMode to force PLINQ to run the query on multiple threads.

In this lecture we are going to revisit those results, analyse what went wrong and attempt to fix the program to produce the correct output.

By the end of the lecture you will understand exactly how PLINQ affects item ordering.

06:44

PLINQ is an extremely powerful library. It valiantly attempts to parallelise every LINQ query you throw at it but sometimes it simply cannot execute a query on multiple threads.

When a query is too complex PLINQ will silently fall back to sequential mode and execute the entire query on a single thread. When this happens PLINQ will actually be slightly slower than regular LINQ!

In this lecture we will identify all the scenarios in which PLINQ falls back to sequential mode. By the end of the lecture you will know which queries should not be parallelised.

04:24

Congratulations on finishing this section. This is a recap of what we have learned.

Using Parallel LINQ
4 questions
Section 5: Final Words
02:57

In this lecture I would like to thank you for finishing the course and offer some final words.

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

Mark Farragher, Microsoft Trainer

Hi I'm Mark, and I am very pleased to meet you!

My IT career spans 2 decades and I've worn many different hats over the years. I am a serial entrepreneur and have launched two startups in The Netherlands. I've also been a CTO three times.

I started working with C# and the .NET framework 15 years ago. Today I live in Barcelona and spend my time creating online courses to share my knowledge of professional C# programming with a wide audience.

Ready to start learning?
Take This Course