Chapter 1.1 C# – The Task Parallel Library

Skill 1.1. Implement multithreading and Async processing.

Can be used for parallel processing. This enables things that dont need to run sequentially can be executed concurrently. Only increases performance when you have a lot of processing that can be executed at the same time. If using on small data sets performance can be harmed.

It contains the following static methods:

  • Invoke – accepts a number of Action delegates and creates a task for each of them
    • Action Delegate – encapsulation of a method that accepts no arguments and doesn’t record a result.
    • It can be written as a Lamba expression  Parallel.Invoke(()=>func1(), ()=>func2());
    • It can start a large number of events at a time, but you cannot control the order of the events run
  • ForEach – loop through a collection of items, a parameter can be added to this collection. Tasks are not completed in the same order they are run.
  • For – Loop from pointer not objects

Manage For and ForEach

  • Break – This will ensure all iterations that are running will be stopped result variable has a value of false and LowestBreakIteration of 500.
  • Stop – This will stop everything from running. result LowestBreakIteration will have a value of null

Working with Boolean

Exclusive OR – only when one side fits the bill

Null Coalescing operator

The ?? operator is called the null-coalescing operator

Can be used to provide a default value for Null fields

For Loops

Used when going through a collection until a specific condition is reached.

    • break – drops out of the for loop
  • continue – moves to the item in the loop

While and do – while loop

Basically, a harder way to write and for loop

When the condition is false it will drop out the loop

Do-while executes at least once even if the condition is false.

Foreach

This goes through each row in a collection and allows you to get info from it safely, it won’t however let you write it back to the database.

Jump statement

Do not use these, but they allow us to jump to different areas of the code

Leave a Reply

Your email address will not be published. Required fields are marked *