Posts Tagged Threads

Keeping The Main Thread Running with the Async CTP Dataflow


Introduction

The previous post presented some code for keeping the main thread of a console application open when the dataflow was being processed. This code I believe is down right wrong, or at least not the best way to achieve the desired result.

The wrong code

private static Task Test6_Main_Wrong()
{
    BroadcastBlock<int> bcBlock = Test6();
    for (int i = 0; i <= 100; i++)
    {
        if (bcBlock.Post(i))
            Debug.WriteLine(
                String.Format("Post of {0} Succeeded", i));
        else Debug.WriteLine(
                String.Format("Post of {0} Not Accepted", i));
    }
    bcBlock.Complete();
    while (bcBlock.Completion.IsCompleted == false)
    {
        Debug.WriteLine("Thread Sleeping");
        Thread.Sleep(1000);
    }
    Debug.WriteLine("Done");
    return bcBlock.Completion;
}

The use of the sleep is what is wrong here. It is an arbitrary value for the amount of the data flow network will take to complete.

The Right Code

private static Task Test6_Main()
{
    BroadcastBlock<int> bcBlock = Test6();
    for (int i = 0; i <= 100; i++)
    {
        if (bcBlock.Post(i))
            Debug.WriteLine(
                String.Format("Post of {0} Succeeded", i));
        else Debug.WriteLine(
                String.Format("Post of {0} Not Accepted", i));
    }
    bcBlock.Complete();
    Debug.WriteLine("Done");
    return bcBlock.Completion;
}

And the main function invocation of the function is:

Test6_Main().Wait();

Key Points:

  • Returning the Task from the function to the main. The Task Object has all the plumbing to know about competition of the task, the framework does all of the heavy lifting here.
  • The Wait method keeps the main thread open while a data flow network of any size will complete.

Conclusions

  • Using Thread.Sleep is bad in Async Dataflow applications.
  • Using Task.Wait looks like a much better way to achieve what I was after.
  • There are other occasions where I have used Sleep. I have solved one of these cases in a much better way, and I will post that example shortly.
Advertisements

, , , ,

Leave a comment

Parallel Programming in .Net – Resources


Introduction

This may be the first of a series of blog post on Parallel Programming with .Net 4.0. It is a “maybe” at this point in time, in that I intended to get into this topic over the coming weeks, but one never knows what else may crop up.

This started off being a look at the synchronised collections support in .Net 4.0, but I’ve decided that I should include some other topics. The other big inclusions are intended to be:

  • Parallel Looping constructs,
  • Parallel LINQ, which the parallel looping leads into, and
  • The Task Class addition to .Net 4.0 Framework.

When you include those, you pretty much have a Parallel programming .Net 4.0 “in a nutshell”. This series may have the impetus to look into some of the more “esoteric”, “subtle” and all-round deeper issues associated with the development, implementation, testing, and deployment of parallel solutions in .Net. But, wait and see, I could come up with some “gems”, and “pearls of wisdom” on the way.

Resources

This is the start of a list of “good” resources on parallel programming in .Net. I’ll try and post further blog posts on the topic as I find more resources (I’ve some PFD’s salted away on the topic as well I’ll try and dig up).

Parallel Programming with Microsoft .NET This is a MSDN Patterns and Practices publication. It contains very good information on parallel programming.

System.Collections.Concurrent Namespace This is the .Net namespace which contains some very useful “pre- cooked” collections which can be used in concurrent (parallel) programming. My preference is to always look at these collections first, and build my own concurrent collection as a last resort.

System.Threading.Tasks Namespace This is the >net namespace which contains the Task object, and the associated infrastructure built into .Net for running parallel Tasks. Again, my preference is to look here first, and only resort to using the Thread Class as a last resort when things do not fit with supplied .Net support.

Parallel Programming in the .NET Framework This is a “top level” page, which has links to much of the .Net Parallel support.

Threading in C# – Part 5 – Parallel Programming This looks to be very good on LINQ and Parallel aspects which can be invoked in the LINQ context. There is also some details on the Task, and associated .Net Framework classes. I’ll be reading this very carefully (again, it will not for the first time I refer to these pages).

Patterns Of Parallel Programming This is a link to a PFD which contains “much” detail on C# .Net Parallel Programming. I’ve yet to digest this one (118 pages is a bit much for one sitting, I’ll have a couple of “bites at this cherry”).

This is just a start of a list of resources. I’ll post further lists of resources, as I dig into the topics I wish to cover.

, , , , , , ,

4 Comments

%d bloggers like this: