Hi Nick,
First I'd say I agree with what Peter said. Now let me share some of my
thoughts.
The short answer to your question is simply a "YES". And now let's take a
closer look at the problem.
Sub problem one: the application is terminating - your Main returns, now is
there any reasons for your object and the thread created by that object
stay alive? If the thread's job is like its name - a watcher for something
- then I don't think so (or please state the reasons).
In this case, the first modification I would make is to mark the thread
created by your object a Background thread. Background threads are
identical to foreground threads, except that background threads do not
prevent a process from terminating. Once all foreground threads belonging
to a process have terminated, the common language runtime ends the process.
Any remaining background threads are stopped and do not complete.
To mark the background thread, set Thread's IsBackground property to true.
Sub problem two: Is the "_watcher" thread executing an instance method (not
static) of the same object (this)? If this is the case, then your object
will never be GCed until the thread terminates. Because the thread holds a
reference to "this" - it is executing an instance method of "this". Thus
even you added the Finalizer, it will not be called until the thread
terminiates.
To solve the problem, don't execute the instance method on the watcher
thread if the only way to let the method return is to set the shutdown flag
in the Dispose method called by the Finalizer, that's a dead-lock.
Sub problem three: In the private Dispose method, calling
this._watcher.Join() is not the right thing to do. What if the thread took
a long time to exit or even worse, never exists (like having a dead-lock)?
Then your Dispose method never returns. Very terrible result. If the
Dispose method is called from the Finalizer, that could mean the GC will
not likely to collect anything anymore.
To make the thread exit, call Abort() which will usually terminates the
thread - unless the thread is being suspended or it cancels the abort. See
http://msdn.microsoft.com/en-us/library/ty8d3wta.aspx for more info.
Hope these information helps.
If you could post more code about your current implementation, it would be
helpful for us helpers to have a clear picture of what's going on there.
Any further questions are welcomed.
Regards,
Jie Wang
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.