The objects used in the thread will be GC'd when then are no longer
referenced by any active code and the GC gets around to running. Its a
good idead to use try ... finally to clean up your non-memory resources
in a timely fashion (without having to wait for the GC and finalization)
such as closing FileStreams, SqlConnections, etc.
However, I have to disagree on one point. I would advise not to call
Thread.Abort in any circumstances unless you are bringing down the
AppDomain or terminating the application. If you do you may hose your
running AppDomain and that is really not a good idea if you intend to
carry on running. If the thread won't stop, log the fact so you can
investigate later (as it probably means you have a bug somewhere) and
leave it as a zombie until your application exits.
Regards
Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<
[email protected]>
Once you break out of your while loop, then your on your way out of the
thread. So process your Close() method after the while loop breaks or in
a
Finally block if your wrapping your while in a Try/Catch. Close down
your
resources (i.e. sockets, web services, etc like normal in your close.
Normal GC will handle any managed resources when they loose their last
reference. As to your first question, I would break on event first if
possible, then Interrupt if can't break out for some reason. Only then
use
Abort if nothing else works. hth
--
William Stacey, MVP
So, if I have a self-sustained business operation that doesn't need
anything
from the thread that started it, I should use Thread.Interrupt()? Once
I
do
that, what else do I need to do to make sure that the objects that
were
instantiated within the thread are GC'ed (maybe a web service
connection,
database connection, or some other high-overhead resource)?
--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/
Normally, in your worker threads you block once waiting on some
blocking
read or other, process the read/write and loop. In these cases, you
may
be
able to useWaitHandle.WaitAny and wait on a cancel ManualResetEvent
event
and your other wait object. That way, if you signal your cancel
handle,
you
can break out clean without needing Abort or Interrupt. So in your
shutdown, you can do that first, test for exit, then do an Interrupt
if
no
shutdown. If still no joy, do the abort as last resort.
--
William Stacey, MVP
But if you know you're going to get rid of the thread permanently,
why
not
call Thread.Abort()? Thread.Interrupt() leaves all the resources in
tact,
and I want to force the GC to collect those resources.
--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/
message
Off the top of my head I'm not sure what would cause the delay,
but
in
general should should not call thread.Abort. It is the .NET
equivelent
of
TerminateThread and has similar issues, for example it may
terminate
a
finally block prematurely. There are only two situations where is
is
OK
to
call Thread.About
1) when you call it on the current thread (so you know what
situation
the
thread is being ripped down in)
2) when you are pulling down the whole AppDomain
Prefer a cooperative mechanism with flags or calling
Thread.Interrupt
Regards
Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#
[email protected]>
I ran into an interesting re-pain delay after calling the Abort()
method
on
a thread, but it only happens the very first time I call it.
Every
time
afterward, there is no delay.
I've got a delegate inside the UI that I call to update the
progress
meter.
I use the Suspend() and Abort() methods based on button events.
I can watch the progress meter increase just fine when the thread
is
running. When I select Start, I enable the Cancel & Pause buttons
(which
call Abort() or Suspend()). The problem happens the very first
time I
click
Cancel (causing Abort() to be called). I change the status of the
Pause
&
Cancel buttons back to disabled and I manually clear the progress
bar.
What I end up seeing is a 3-4 second delay between the button
click
and
the
actual clearing of the progress bar and disabling of the buttons.
I
checked
the button_click event handler, and it runs through fine without
delay.
It
looks as if there's some kind of delay between the event and the
re-paint,
but it only happens the very first time. If I Start and Cancel
again,
the
repaint happens immediately (as I would expect).
I don't have any timers or anything else running that would cause
the
re-paint to be intercepted. Is there something about calling
Abort()
the
very first time that would delay a re-paint (maybe by having to
invoke
the
GC for the first time for the app or something)?
--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (
http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004
[microsoft.public.dotnet.languages.csharp]
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (
http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004
[microsoft.public.dotnet.languages.csharp]