Beginners Guide To Threading, C#

Task Parallel Library 1 of n

I have just started work on what will be a 6 part series of articles on an in depth look into using the .NET 4 Task Parallel Library (TPL).

I have just published the 1st part in this series of article which is available using this link:

  • This article covers the following topics:
  • Comparing Threads and Tasks
  • Starting Tasks
  • Handling Exceptions With Tasks
  • Cancelling Tasks
  • UI Synchronization Using Tasks

I hope it has something there for you, and i hope you read the rest of the articles when I get them up.

6 thoughts on “Task Parallel Library 1 of n

    1. Barry thanks for these links. I was already going to cover custom schedulers, but was not going to write about scoping, so thats a good one, thanks for you links.

  1. I had the specific case where I used the limited-concurrency scheduler (for long copying tasks, so I don’t thrash the local disk or flood the network), and wanted to queue up tasks synchronously. Pretend Task.Factory can only run one task at a time (e.g. single core PC):

    void foo()
    Widget w1 = new Widget(arg1, arg2)
    Widget w2 = new Widget(arg3, arg4)

    Task.Factory.StartNew( () => {
    w1.Run(); // stays in scope, as per MS webpage in other comment

    // don’t run until w1 has finished.
    Task.Factory.StartNew( () => {
    w2.Run(); // stays in scope, as per MS webpage in other comment

    } // w1 and w2 now out of scope, and w2 might not have started yet.

    Running w2 worried me for a little while (ex-C++ programmer), until I dug up the MS link in previous comment.

  2. I should probably add that whilst I could create the objects within the lambda expression, which would partially solve the scoping problem, the arguments might still be scoped locally to foo(), which is still the same scoping issue. Either way, for a someone who is a bit new to C# still, it was a little confusing until I found the right webpage. It just might be worth clarifying it for other people when they read the article. 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s