Calling Dispose Instance Method

Is it correct that the public method calls Dispose of IDisposable in the same class?

eg.

public class Worker : IDisposable
{
    public void Close()
    {
       SendCloseCommand();
       Dispose();
    }

    public void Dispose()
    {
       ////other resources release
    }

    private void SendCloseCommand()
    {
    }
}

      

One more thing about the one-off schema: If Close should always be called, is it better to call from the Dispose method or not?

If not, I have to put the use of the Worker class in try / catch / finally everywhere to call Close. Right?

The correct design should be?

public class Worker : IDisposable
{
    public void Close()
    {
       SendCloseCommand();
    }

    public void Dispose()
    {
       Close();
       ////other resources release
    }

    private void SendCloseCommand()
    {
        ////other stuff
    }
}

      

+3


source to share


2 answers


If you look at Microsoft's Implementation forStreamReader

, Dispose

is called from Close

,

public override void Close()
{
    Dispose(true);
}

      

and the implementation for Dispose

also closes the thread by calling the Close

base Stream

.

protected override void Dispose(bool disposing)
{
    // Dispose of our resources if this StreamReader is closable.
    // Note that Console.In should be left open.
    try {
        // Note that Stream.Close() can potentially throw here. So we need to 
        // ensure cleaning up internal resources, inside the finally block.  
        if (!LeaveOpen && disposing && (stream != null))
            stream.Close();
    }
    finally {
        if (!LeaveOpen && (stream != null)) {
            stream = null;
            encoding = null;
            decoder = null;
            byteBuffer = null;
            charBuffer = null;
            charPos = 0;
            charLen = 0;
            base.Dispose(disposing);
        }
    }
}

      



In your class implementation, I would call Dispose

from your method Close

, as you do in your first code snippet. In Dispose

I would check the status Worker

if it is not closed then close it using SendCloseCommand

and then free the resources.

public void Dispose()
{
   if(this.Status != Closed) // check if it is not already closed
     {
         SendCloseCommand();
     }

   ////other resources release
}

      

This will ensure that resources are removed even if your class is used with a statement using

or if someone calls it Close

manually.

Remember to check the status of your object Worker

before issuing the Close command.

+4


source


Microsoft has suggestions for using a method Close

for a class that implements IDisposable

. This is part of the Dispose Pattern :

CONSIDER , which provides a method Close()

, in addition to Dispose()

if close is standard scope terminology.

In doing so, it is important that the implementation is Close

identical with Dispose

and considers the use of the method IDisposable.Dispose

explicitly.

public class Stream : IDisposable {
    IDisposable.Dispose(){
        Close();
    }
    public void Close(){
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

      



So, Microsoft's suggestion is to hide Dispose

and let it call Close

for the actual cleanup. Also, remember the guide for a few calls Dispose

:

DOs allow a method to be called Dispose(bool)

more than once. After the first call, the method may decide to do nothing.

By following these guidelines, your code is compatible with the .NET library, and you will avoid confusion about whether your class must be closed or removed for proper cleanup.

+2


source







All Articles