List Info

Thread: Re: Storing shared secrets




Thread implementation issue
country flaguser name
United States
2008-03-28 09:58:05
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


Re: Thread implementation issue
user name
2008-03-28 10:27:35
Peter Osucha <PeterPROTEINDISCOVERY.COM> wrote:

> 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. 

> 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.

> 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.

That's because your thread isn't waiting for Blinker to
finish. You
could call .Join on the thread object (and live with a delay
of up to
250ms), or alternatively, instead of using Thread.Sleep, use
a
ManualResetEvent. Signal the event for shutdown, and use the
WaitOne
with a 250ms timeout for your blinking pause. The return
value of
WaitOne will tell you whether the event was signalled or
not; the thread
should quit when WaitOne returns true.

You should still call .Join on the thread, if you want to
guarantee that
the thread has finished before the application shuts down.

-- Barry

-- 
http://barrkel.blogspot.
com/

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

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


Re: Thread implementation issue
user name
2008-03-28 10:16:55
> 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?

Of course you can set the Stop property from Dispose, just
as you can
set it from every other method. (As long as setting the
field is
synchronized by a lock or the field is volatile.) You just
need to
call Dispose before the application exits. It won't be
called
automatically, you have to manually invoke it, for example
via a C#
"using" statement.

Some classes have finalizers automatically calling Dispose
when the GC
comes to collect instances of those classes. However, this
won't work
in your scenario as the thread will probably keep the
instance alive
so that the GC can't collect it.

The point is that there are two types of threads. Those
which keep an
application alive, just as a "main thread" does;
and those which
don't. The latter are called background threads. So if you
want the
latter's behavior, create a background thread. If you don't
want a
background thread, you need to make it stop explicitly.

Fabian

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

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


Re: Thread implementation issue
user name
2008-03-28 10:27:35
Peter Osucha <PeterPROTEINDISCOVERY.COM> wrote:

> 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. 

> 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.

> 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.

That's because your thread isn't waiting for Blinker to
finish. You
could call .Join on the thread object (and live with a delay
of up to
250ms), or alternatively, instead of using Thread.Sleep, use
a
ManualResetEvent. Signal the event for shutdown, and use the
WaitOne
with a 250ms timeout for your blinking pause. The return
value of
WaitOne will tell you whether the event was signalled or
not; the thread
should quit when WaitOne returns true.

You should still call .Join on the thread, if you want to
guarantee that
the thread has finished before the application shuts down.

-- Barry

-- 
http://barrkel.blogspot.
com/

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

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


Re: Thread implementation issue
user name
2008-03-28 10:16:55
> 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?

Of course you can set the Stop property from Dispose, just
as you can
set it from every other method. (As long as setting the
field is
synchronized by a lock or the field is volatile.) You just
need to
call Dispose before the application exits. It won't be
called
automatically, you have to manually invoke it, for example
via a C#
"using" statement.

Some classes have finalizers automatically calling Dispose
when the GC
comes to collect instances of those classes. However, this
won't work
in your scenario as the thread will probably keep the
instance alive
so that the GC can't collect it.

The point is that there are two types of threads. Those
which keep an
application alive, just as a "main thread" does;
and those which
don't. The latter are called background threads. So if you
want the
latter's behavior, create a background thread. If you don't
want a
background thread, you need to make it stop explicitly.

Fabian

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

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


Re: Thread implementation issue
user name
2008-03-28 10:44:31
> 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?
>

Did you implement a Finalize as well, which calls your
Dispose method?

Without a Finalize, nobody is going to call your Dispose
method for you.

// Ryan

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

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


Re: Thread implementation issue
user name
2008-03-28 10:50:58
On Fri, Mar 28, 2008 at 11:44 AM, Ryan Heath
<ryan.q.heathgmail.com> wrote:

>
> Did you implement a Finalize as well, which calls your
Dispose method?
>
> Without a Finalize, nobody is going to call your
Dispose method for you.
>

+1 to what Ryan said.  Without a Finalize it would be up to
you to call
Blinker.Dispose() after you stop it (if you'd want to
dispose of it after
stopping that is).


-Pete

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

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


Re: Thread implementation issue
user name
2008-03-28 10:44:31
> 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?
>

Did you implement a Finalize as well, which calls your
Dispose method?

Without a Finalize, nobody is going to call your Dispose
method for you.

// Ryan

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

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


Re: Thread implementation issue
user name
2008-03-28 10:50:58
On Fri, Mar 28, 2008 at 11:44 AM, Ryan Heath
<ryan.q.heathgmail.com> wrote:

>
> Did you implement a Finalize as well, which calls your
Dispose method?
>
> Without a Finalize, nobody is going to call your
Dispose method for you.
>

+1 to what Ryan said.  Without a Finalize it would be up to
you to call
Blinker.Dispose() after you stop it (if you'd want to
dispose of it after
stopping that is).


-Pete

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

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


Re: Thread implementation issue
country flaguser name
United States
2008-03-28 11:03:51
No, I didn't.  That would do the trick.  

The code in the 'Go' method of Blinker is...

        public void Go (  )
        {
            _stop = false;
            while ( !_stop )
            {
                SetPinState(on);
                Thread.Sleep ( TimeSpan.FromMilliseconds (
_onIntervalMs
) );
                SetPinState(off);
                Thread.Sleep ( TimeSpan.FromMilliseconds (
_offIntervalMs ) );
            }
        }

        public void Stop ()
        {
            _stop = true;
        }




-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Peter
Vertes
Sent: Friday, March 28, 2008 11:51 AM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Thread implementation issue

On Fri, Mar 28, 2008 at 11:44 AM, Ryan Heath
<ryan.q.heathgmail.com>
wrote:

>
> Did you implement a Finalize as well, which calls your
Dispose method?
>
> Without a Finalize, nobody is going to call your
Dispose method for
you.
>

+1 to what Ryan said.  Without a Finalize it would be up to
you to call
Blinker.Dispose() after you stop it (if you'd want to
dispose of it
after
stopping that is).


-Pete

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

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


[1-10] [11-20] [21-25]

about | contact  Other archives ( Real Estate discussion Medical topics )