A bit long but I am trying to clearly explain what I have done and the
issue that has arisen...

In working on implementing the Producer/Consumer queue from comments
earlier this week, I am doing much more (and needed) reading on threads
and thread safety.

As a test example, I have an IO line that I want to 'blink' (turn on and
off for 250ms each).  I figured I could easily do this with a separate
thread.  So in a class instance of my main app, I create a class
(Blinker)  to do this 'blinking'.  Blinker has a few properties set
through its constructor.  It also has a 'Go' method (no parameters) -
which sets up a while loop that continuously blinks the IO line using
Sleep() statements - and one other property 'Stop' that can be set to
'true' to get out of the loop.  OK so far.

So I set up a class instance of this 'Blinker' class in my main app
class (this is so I'll always have a class reference to the Blinker
object so I can get out of the loop, thus allowing a thread that is
running the class to exit).  When I want to get the blinking going, I do
the following.

            Thread blinkerThread = new Thread ( new ThreadStart (
_blinker.Go ) );
            blinkerThread.Name = "blinkerThread";
            blinkerThread.Start();

This works fine and I can kill the blinkerThread by setting Blinker.Stop
= true.

Now to my issue...

If I get the blinker going by way of this thread and then close my app
without 'Stop'ing the blinker, the blinkerThread is still alive thereby
not allowing my app to shut down correctly.  I can set the IsBackground
property of the blinkerThread to true - this will make sure that once
the main thread closes, the blinkerThread (if active) will close as
well.

If I didn't want to set the 'IsBackgroud' property of the thread, I
figured I could just implement IDisposable in my class and set
Blinker.Stop=true in Dispose().  However, that didn't seem to work.
Should it have worked?  How else might I ensure that the blinkerThread
is ended correctly?

Sincerely,
Peter

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to