Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 17th, 2004, 09:51 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default Threads and an objects lifetime

Hi all,

I have a problem with a class I've wrote. Basically the class opens a windows resource and runs a thread that handles events of that resource by calling methods on the object.

The problem I have is that although the object can be explicitly closed to free the resources and end the thread, I've included code in the destructor for this purpose just in case the user forgets (a sort of failsafe). Unfortunately the thread keeps the object alive.

Has anyone else had this problem and is there a way round it?

Basic code sample:

public class MyClass : IDisposable
{
    private Thread eventThread;
    // thread routine
    private void EventHandler()
    {
        try
        {
            while(true)
            {
                // check for events
                WhenSomething();

                // **** this loop keeps the object alive ****
            }
        }
        catch(ThreadAbortException)
        {
            // do nothing (exit normally)
        }
        catch
        {
            // expose exception to main thread
        }
    }
    private void Start()
    {
        eventThread = new Thread(new ThreadStart(EventHandler));
        eventThread.Priority = ThreadPriority.AboveNormal;
        eventThread.Start();
    }
    ~MyClass()
    {
        // just in-case IDisposable.Dispose() is not called
        Dispose(true);
    }
    protected void Dispose(bool fromDestructor)
    {
        // free unmanaged resources
        if(eventThread != null)
        {
            // abort the event thread
            eventThread.Abort();
            eventThread.Join();
            eventThread = null;
        }
    }
    protected void WhenSomething()
    {
        // call event handler if there is one
    }
    public static MyClass Open()
    {
        MyClass result = new MyClass();
        result.Start();
        return result;
    }
    public void Dispose()
    {
        Dispose(false);
        Gc.SuppressFinalize(this);
    }
}
// IN ANOTHER FILE
public class TestMyClass
{
    public static void Main(string[] args)
    {
        MyClass test = Open();

        // run some tests

        // **** when this method ends I thought the 'test' object
        // would go out of scope and be destroyed but the thread
        // keeps it alive ****
    }
}

Thanks, I hope it's not too long winded.
Kep.
__________________
Kep.
 
Old May 18th, 2004, 12:36 AM
Authorized User
 
Join Date: Apr 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Just make sure that the condition changes to false otherwise your loop (in EventHandler) will keep on creating the object. Because I can't see where you change it to false. Hope this helps

while(true)
{
   // check for events
      WhenSomething();

  // **** this loop keeps the object alive ****
}

Jan

dude_in_africa
 
Old May 18th, 2004, 01:05 AM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the response.

I terminate the Thread by calling
Code:
Thread.Abort()
from the class destructor. I thought this would throw a ThreadAbortException in the thread method. But the class destructor does not get called.

I've also tried messing around with
Code:
Thread.IsBackground = true
to try and get the application to terminate but this doesn't seem to work either.
 
Old May 18th, 2004, 01:11 AM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've just worked it out.

You need to set the thread to a background thread with
Code:
MyThread.IsBackground = true
and make sure that the ThreadStart delegate is non-static.

Code:
private void EventHandler()
{
    while(true)
    {
        // do some work!
    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Hashtable and lifetime of objects inside luca C# 3 February 19th, 2007 07:33 AM
CCommand Lifetime comdev2001 Oracle 0 November 29th, 2005 12:13 PM
My week with threads freezotic BOOK: Beginning Java 2 0 April 16th, 2004 04:27 PM
Threads Help hybrid_dev C# 0 July 31st, 2003 10:23 AM
Problems with threads. Threads disappearing? DriesNeyrinck VB.NET 0 June 10th, 2003 08:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.