Library tutorials & articles

.NET Threading Part I

Synchronization Objects

The previous code contains some rather inefficient coding when the main thread cleans up. I repeatedly test the queue length every second until the queue length reaches zero. This may mean that the process will continue executing for up to a full second after the queues are finally drained. Wow! I can't have that. OK! Maybe that's not a good reason to change the code, but it is a convenient excuse for me to introduce you to the System.Threading.ManualResetEvent class. Using a ManualResetEvent object, I could trigger the main thread to complete as soon as the last ware was consumed. I'll do this by creating two new instance data members, a bool WaitForComplete to tell us when the main thread is waiting to exit and a ManualResetEvent Event object that will signal the main thread to exit.

public void Wait()
{
    if (QueueLength == 0)
    {
        return;
    }
    Event = new ManualResetEvent(false);
    WaitForComplete = true;
    Event.WaitOne();
}

public void Consume(Object obj)
{
    Console.WriteLine("Thread {0} consumes {1}",
        Thread.CurrentThread.GetHashCode(), //{0}
        ((Ware) obj).id); //{1}
    Thread.Sleep(100);
    QueueLength--;
    if (WaitForComplete)
    {
        if (QueueLength == 0)
        {
            Event.Set();
        }
    };
}

When the consuming thread finishes consuming a ware and detects that the WaitForComplete is true, it will trigger the Event when the queue length is zero. Instead of calling the while block when it wants to exit, the main thread calls the Wait instance method. This method sets the WaitForComplete flag and waits on the Event object.

Comments

  1. 27 Feb 2004 at 22:06

    I was asked to do the following:
    write the method call to begin running the thread and begin processing
    (a) namespace = testProject
    (b) form to run = frmMain


    All the things that I have read on threading so far say that a thread point to a function of whatever comes after the "addressOf" in the argument.  Therefore, I don't understand what (a) and (b) are trying to refer to or specify.  If you happen to understand what that question is looking for, or know any good literature I can look at for the given topic, please let me know.  Thank you.
    email me

  2. 04 Nov 2003 at 15:04
    Quote:
    [1]Posted by James Crowley on 4 Nov 2003 02:52 PM[/1]
    Are you ensuring that the page doesn't finish loading before both threads have returned a result? Otherwise, you may find that ASP.NET is outputting the page to the client before the thread has actually called the callback!



    Yes...and if I may add, rather hesitantly at this juncture, I did think about that.
    What surprised me however was when I ran this in debugging mode, I as able to trace both callbacks
    and in one case the dataset returned had no data. I wonder if  there are any issues with returning non-simple types
    in the event driven call back. May be, using the delegate mechanism may work. I havn't done
    much callback based threading so far...
  3. 04 Nov 2003 at 14:52
    Are you ensuring that the page doesn't finish loading before both threads have returned a result? Otherwise, you may find that ASP.NET is outputting the page to the client before the thread has actually called the callback!
  4. 04 Nov 2003 at 13:20
    I have tried threading in my web page. I tried to fill two drop-downs by threading two methods in two different business layer objects from my web page. I used the event call back method  to return two datasets (one for each drop down) but got  inconsistent results..drop-downs were populated and sometimes not. Any ideas?
  5. 01 Jan 1999 at 00:00

    This thread is for discussions of .NET Threading Part I.

Leave a comment

Sign in or Join us (it's free).

Randy Charles Morin Randy's article are Copyright 1998-2003 Randy Charles Morin

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

Want to stay in touch with what's going on? Follow us on twitter!