HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Could I dispose IDisposable objects that are part of a Thread?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
objectsarepartcouldthatthreaddisposeidisposable

Problem

I have a question about using an IDisposable object in the ThreadStart function. I have a class that derives from ServiceBase:

public class ServiceTest: System.ServiceProcess.ServiceBase
{
    private System.ComponentModel.Container components = null;
    private System.Threading.Thread         m_cProcessThread;

    public ServiceTest()
    {
        this.ServiceName = "ServiceTest";
    }

    static void Main()
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun  = new System.ServiceProcess.ServiceBase[] { new ServiceTest() };

        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
    protected override void Dispose( bool disposing )
    {
        if ( disposing )
        {
            if (components != null) 
            {
                components.Dispose();
            }
        }
        base.Dispose( disposing );
    }

    protected override void OnStart(string[] args)
    {
        ProcessThread   cThread;

        cThread = new ProcessThread();
        m_cProcessThread = new Thread(new ThreadStart(cThread.InitProcess));
        m_cProcessThread.Start();
    }

    protected override void OnStop()
    {
        m_cProcessThread.Abort();
    }
}


and in the OnStart function I have the ProcessThread object which is an IDisposable object. I was wondering if it is safe to re-write the OnStart function to do this:

protected override void OnStart(string[] args)
    {
        using (Process cThread = new ProcessThread())
        {
            m_cProcessThread = new Thread(new ThreadStart(cThread.InitProcess));
        }

        m_cProcessThread.Start();
    }


Or will there be any consequences in doing this? I have a bit of an OCD when I see IDisposable objects in C# code that makes me believe that they HAVE to be disposed as soon as you are done lol. Also I don't know if my question title really fits with what I'm asking so if you could come up with a better title then feel free to edit.

Code is targeted at

Solution

Strictly speaking, it depends on what InitProcess does. It may very well work OK as long as it never touches resources in the ProcessThread class which get disposed.

However, even if it works this time, it is a bad idea. Passing a method group from a class which gets disposed before the thread even starts is asking for trouble.

A better option would be to create a method inside your ServiceTest class that the thread executes. Inside that thread, you can instantiate a ProcessThread object, call InitProcess, and ensure that Dispose is only called once InitProcess has completed:

protected override void OnStart(string[] args)
{
    m_cProcessThread = new Thread(InitProcess);
    m_cProcessThread.Start();
}

private void InitProcess()
{
    using(var processThread = new ProcessThread())
    {
        processThread.InitProcess();
    }
}


You may also note that I removed the explicit construction of a ThreadStart delegate. In many cases, you can pass a method group directly where delegates are called for. I find it a little cleaner to do so.

Code Snippets

protected override void OnStart(string[] args)
{
    m_cProcessThread = new Thread(InitProcess);
    m_cProcessThread.Start();
}

private void InitProcess()
{
    using(var processThread = new ProcessThread())
    {
        processThread.InitProcess();
    }
}

Context

StackExchange Code Review Q#69102, answer score: 7

Revisions (0)

No revisions yet.