IDisposable IDisposable IDisposable IDisposable Interface

定義

アンマネージ リソースを解放するためのメカニズムを提供します。Provides a mechanism for releasing unmanaged resources.

public interface class IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public interface IDisposable
type IDisposable = interface
Public Interface IDisposable
派生
属性

次の例では、実装するリソース クラスを作成する方法、IDisposableインターフェイス。The following example demonstrates how to create a resource class that implements the IDisposable interface.

#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;

// The following example demonstrates how to create a class that 
// implements the IDisposable interface and the IDisposable.Dispose
// method with finalization to clean up unmanaged resources. 
//
public ref class MyResource: public IDisposable
{
private:

   // Pointer to an external unmanaged resource.
   IntPtr handle;

   // A managed resource this class uses.
   Component^ component;

   // Track whether Dispose has been called.
   bool disposed;

public:
   // The class constructor.
   MyResource( IntPtr handle, Component^ component )
   {
      this->handle = handle;
      this->component = component;
      disposed = false;
   }

   // This method is called if the user explicitly disposes of the
   // object (by calling the Dispose method in other managed languages, 
   // or the destructor in C++). The compiler emits as a call to 
   // GC::SuppressFinalize( this ) for you, so there is no need to 
   // call it here.
   ~MyResource() 
   {
      // Dispose of managed resources.
      component->~Component();

      // Call C++ finalizer to clean up unmanaged resources.
      this->!MyResource();

      // Mark the class as disposed. This flag allows you to throw an
      // exception if a disposed object is accessed.
      disposed = true;
   }

   // Use interop to call the method necessary to clean up the 
   // unmanaged resource.
   //
   [System::Runtime::InteropServices::DllImport("Kernel32")]
   static Boolean CloseHandle( IntPtr handle );

   // The C++ finalizer destructor ensures that unmanaged resources get
   // released if the user releases the object without explicitly 
   // disposing of it.
   //
   !MyResource()
   {      
      // Call the appropriate methods to clean up unmanaged 
      // resources here. If disposing is false when Dispose(bool,
      // disposing) is called, only the following code is executed.
      CloseHandle( handle );
      handle = IntPtr::Zero;
   }

};

void main()
{
   // Insert code here to create and use the MyResource object.
   MyResource^ mr = gcnew MyResource((IntPtr) 42, (Component^) gcnew Button());
   mr->~MyResource();
}
using System;
using System.ComponentModel;

// The following example demonstrates how to create
// a resource class that implements the IDisposable interface
// and the IDisposable.Dispose method.

public class DisposeExample
{
    // A base class that implements IDisposable.
    // By implementing IDisposable, you are announcing that
    // instances of this type allocate scarce resources.
    public class MyResource: IDisposable
    {
        // Pointer to an external unmanaged resource.
        private IntPtr handle;
        // Other managed resource this class uses.
        private Component component = new Component();
        // Track whether Dispose has been called.
        private bool disposed = false;

        // The class constructor.
        public MyResource(IntPtr handle)
        {
            this.handle = handle;
        }

        // Implement IDisposable.
        // Do not make this method virtual.
        // A derived class should not be able to override this method.
        public void Dispose()
        {
            Dispose(true);
            // This object will be cleaned up by the Dispose method.
            // Therefore, you should call GC.SupressFinalize to
            // take this object off the finalization queue
            // and prevent finalization code for this object
            // from executing a second time.
            GC.SuppressFinalize(this);
        }

        // Dispose(bool disposing) executes in two distinct scenarios.
        // If disposing equals true, the method has been called directly
        // or indirectly by a user's code. Managed and unmanaged resources
        // can be disposed.
        // If disposing equals false, the method has been called by the
        // runtime from inside the finalizer and you should not reference
        // other objects. Only unmanaged resources can be disposed.
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if(!this.disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if(disposing)
                {
                    // Dispose managed resources.
                    component.Dispose();
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                // If disposing is false,
                // only the following code is executed.
                CloseHandle(handle);
                handle = IntPtr.Zero;

                // Note disposing has been done.
                disposed = true;

            }
        }

        // Use interop to call the method necessary
        // to clean up the unmanaged resource.
        [System.Runtime.InteropServices.DllImport("Kernel32")]
        private extern static Boolean CloseHandle(IntPtr handle);

        // Use C# destructor syntax for finalization code.
        // This destructor will run only if the Dispose method
        // does not get called.
        // It gives your base class the opportunity to finalize.
        // Do not provide destructors in types derived from this class.
        ~MyResource()
        {
            // Do not re-create Dispose clean-up code here.
            // Calling Dispose(false) is optimal in terms of
            // readability and maintainability.
            Dispose(false);
        }
    }
    public static void Main()
    {
        // Insert code here to create
        // and use the MyResource object.
    }
}
Imports System
Imports System.ComponentModel

' The following example demonstrates how to create
' a resource class that implements the IDisposable interface
' and the IDisposable.Dispose method.
Public Class DisposeExample

   ' A class that implements IDisposable.
   ' By implementing IDisposable, you are announcing that 
   ' instances of this type allocate scarce resources.
   Public Class MyResource
      Implements IDisposable
      ' Pointer to an external unmanaged resource.
      Private handle As IntPtr
      ' Other managed resource this class uses.
      Private component As component
      ' Track whether Dispose has been called.
      Private disposed As Boolean = False

      ' The class constructor.
      Public Sub New(ByVal handle As IntPtr)
         Me.handle = handle
      End Sub

      ' Implement IDisposable.
      ' Do not make this method virtual.
      ' A derived class should not be able to override this method.
      Public Overloads Sub Dispose() Implements IDisposable.Dispose
         Dispose(True)
         ' This object will be cleaned up by the Dispose method.
         ' Therefore, you should call GC.SupressFinalize to
         ' take this object off the finalization queue 
         ' and prevent finalization code for this object
         ' from executing a second time.
         GC.SuppressFinalize(Me)
      End Sub

      ' Dispose(bool disposing) executes in two distinct scenarios.
      ' If disposing equals true, the method has been called directly
      ' or indirectly by a user's code. Managed and unmanaged resources
      ' can be disposed.
      ' If disposing equals false, the method has been called by the 
      ' runtime from inside the finalizer and you should not reference 
      ' other objects. Only unmanaged resources can be disposed.
      Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
         ' Check to see if Dispose has already been called.
         If Not Me.disposed Then
            ' If disposing equals true, dispose all managed 
            ' and unmanaged resources.
            If disposing Then
               ' Dispose managed resources.
               component.Dispose()
            End If

            ' Call the appropriate methods to clean up 
            ' unmanaged resources here.
            ' If disposing is false, 
            ' only the following code is executed.
            CloseHandle(handle)
            handle = IntPtr.Zero

            ' Note disposing has been done.
            disposed = True

         End If
      End Sub

      ' Use interop to call the method necessary  
      ' to clean up the unmanaged resource.
      <System.Runtime.InteropServices.DllImport("Kernel32")> _
      Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean]
      End Function

      ' This finalizer will run only if the Dispose method 
      ' does not get called.
      ' It gives your base class the opportunity to finalize.
      ' Do not provide finalize methods in types derived from this class.
      Protected Overrides Sub Finalize()
         ' Do not re-create Dispose clean-up code here.
         ' Calling Dispose(false) is optimal in terms of
         ' readability and maintainability.
         Dispose(False)
         MyBase.Finalize()
      End Sub
   End Class

   Public Shared Sub Main()
      ' Insert code here to create
      ' and use the MyResource object.
   End Sub

End Class

注釈

このインターフェイスの主な用途は、アンマネージ リソースを解放することです。The primary use of this interface is to release unmanaged resources. ガベージ コレクターは、自動的にそのオブジェクトを使用できなくする場合は、マネージ オブジェクトに割り当てられたメモリを解放します。The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. ただし、ガベージ コレクションが行われるタイミングを予測することはできません。However, it is not possible to predict when garbage collection will occur. さらに、ガベージ コレクターは、ウィンドウのハンドルなどのアンマネージ リソースの知識がないか、ファイルおよびストリームを開きます。Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

使用して、Disposeガベージ コレクターにアンマネージ リソースを明示的に解放するには、このインターフェイスのメソッド。Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. オブジェクトのコンシューマーは、オブジェクトが不要になったとき、このメソッドを呼び出すことができます。The consumer of an object can call this method when the object is no longer needed.

警告

重大な変更を追加するのには、IDisposable既存のクラスにインターフェイス。It is a breaking change to add the IDisposable interface to an existing class. 型の既存のコンシューマーが呼び出すことはできませんのでDispose、型で保持されているアンマネージ リソースがリリースされることは確信できません。Because pre-existing consumers of your type cannot call Dispose, you cannot be certain that unmanaged resources held by your type will be released.

IDisposable.Disposeインスタンスによって所有されているリソースが不要になったときの実装は、型のコンシューマーによって呼び出されますで管理オブジェクトをラップする必要がありますか、 SafeHandle (推奨される代替)をオーバーライドする必要がありますまたはObject.Finalizeを呼び出すコンシューマーを忘れた場合、アンマネージ リソースを解放するDisposeします。Because the IDisposable.Dispose implementation is called by the consumer of a type when the resources owned by an instance are no longer needed, you should either wrap the managed object in a SafeHandle (the recommended alternative), or you should override Object.Finalize to free unmanaged resources in the event that the consumer forgets to call Dispose.

重要

C++ コンパイラ、.NET Framework でリソースの確定的な破棄をサポートしているし、の直接の実装ではできません、Disposeメソッド。In the .NET Framework, the C++ compiler supports deterministic disposal of resources and does not allow direct implementation of the Dispose method.

方法の詳細についてはこのインターフェイスとObject.Finalizeメソッドを使用してを参照してください、ガベージ コレクションDispose メソッドの実装トピック。For a detailed discussion about how this interface and the Object.Finalize method are used, see the Garbage Collection and Implementing a Dispose Method topics.

IDisposable を実装するオブジェクトを使用します。Using an object that implements IDisposable

アプリが単純に実装するオブジェクトを使用する場合、IDisposableインターフェイス、オブジェクトを呼び出す必要がありますIDisposable.Dispose使用が終了したら、実装します。If your app simply uses an object that implements the IDisposable interface, you should call the object's IDisposable.Dispose implementation when you are finished using it. に応じて、プログラミング言語には、これには 2 つの方法のいずれかの操作を行います。Depending on your programming language, you can do this in one of two ways:

  • 言語を使用して構築など、 using C# および Visual Basic でのステートメント。By using a language construct such as the using statement in C# and Visual Basic.

  • 呼び出しをラップすることによって、IDisposable.Disposeでの実装をtry / finallyブロックします。By wrapping the call to the IDisposable.Dispose implementation in a try/finally block.

注意

ドキュメント型を実装IDisposable事実に注意してくださいし、アラームを呼び出してそのDispose実装します。Documentation for types that implement IDisposable note that fact and include a reminder to call its Dispose implementation.

C# および Visual Basic の Using ステートメントThe C# and Visual Basic Using statement

など、言語にコンストラクトがサポートしている場合、を使用して(C#) ステートメント、 Using Visual Basic でステートメントを使用できますを明示的に呼び出すのではなくIDisposable.Dispose自分で。If your language supports a construct such as the using statement in C# and the Using statement in Visual Basic, you can use it instead of explicitly calling IDisposable.Dispose yourself. 次のコードの例では、この方法を定義する、WordCountファイルとその中の単語の数に関する情報を保持するクラス。The following example uses this approach in defining a WordCount class that preserves information about a file and the number of words in it.

using System;
using System.IO;
using System.Text.RegularExpressions;

public class WordCount
{
   private String filename = String.Empty;
   private int nWords = 0;
   private String pattern = @"\b\w+\b"; 

   public WordCount(string filename)
   {
      if (! File.Exists(filename))
         throw new FileNotFoundException("The file does not exist.");
      
      this.filename = filename;
      string txt = String.Empty;
      using (StreamReader sr = new StreamReader(filename)) {
         txt = sr.ReadToEnd();
      }
      nWords = Regex.Matches(txt, pattern).Count;
   }
   
   public string FullName
   { get { return filename; } }
   
   public string Name
   { get { return Path.GetFileName(filename); } }
   
   public int Count 
   { get { return nWords; } }
}   
Imports System.IO
Imports System.Text.RegularExpressions

Public Class WordCount
   Private filename As String
   Private nWords As Integer
   Private pattern As String = "\b\w+\b" 

   Public Sub New(filename As String)
      If Not File.Exists(filename) Then
         Throw New FileNotFoundException("The file does not exist.")
      End If   
      
      Me.filename = filename
      Dim txt As String = String.Empty
      Using sr As New StreamReader(filename)
         txt = sr.ReadToEnd()
      End Using
      nWords = Regex.Matches(txt, pattern).Count
   End Sub
   
   Public ReadOnly Property FullName As String
      Get
         Return filename
      End Get   
   End Property
   
   Public ReadOnly Property Name As String
      Get
         Return Path.GetFileName(filename)
      End Get   
   End Property
   
   Public ReadOnly Property Count As Integer
      Get
         Return nWords
      End Get
   End Property
End Class

usingステートメントは実際に構文上便利な。The using statement is actually a syntactic convenience. 言語コンパイラは、コンパイル時の中間言語 (IL) を実装して、 try / finallyブロックします。At compile time, the language compiler implements the intermediate language (IL) for a try/finally block.

詳細については、usingステートメントを参照してください、 Using ステートメントまたはステートメントを使用してトピック。For more information about the using statement, see the Using Statement or using Statement topics.

Try/finally ブロックThe Try/Finally block

使用するプログラミング言語がのようなコンストラクトをサポートしないかどうか、 using C# または Visual Basic では、ステートメントでそれを使用していない場合を呼び出すことができます、IDisposable.Disposeから実装、finallyのブロックをtry/finallyステートメント。 If your programming language does not support a construct like the using statement in C# or Visual Basic, or if you prefer not to use it, you can call the IDisposable.Dispose implementation from the finally block of a try/finally statement. 次の例は、usingブロックの前の例では、 try/finallyブロックします。 The following example replaces the using block in the previous example with a try/finally block.

using System;
using System.IO;
using System.Text.RegularExpressions;

public class WordCount
{
   private String filename = String.Empty;
   private int nWords = 0;
   private String pattern = @"\b\w+\b"; 

   public WordCount(string filename)
   {
      if (! File.Exists(filename))
         throw new FileNotFoundException("The file does not exist.");
      
      this.filename = filename;
      string txt = String.Empty;
      StreamReader sr = null;
      try {
         sr = new StreamReader(filename);
         txt = sr.ReadToEnd();
      }
      finally {
         if (sr != null) sr.Dispose();     
      }
      nWords = Regex.Matches(txt, pattern).Count;
   }
   
   public string FullName
   { get { return filename; } }
   
   public string Name
   { get { return Path.GetFileName(filename); } }
   
   public int Count 
   { get { return nWords; } }
}   
Imports System.IO
Imports System.Text.RegularExpressions

Public Class WordCount
   Private filename As String
   Private nWords As Integer
   Private pattern As String = "\b\w+\b" 

   Public Sub New(filename As String)
      If Not File.Exists(filename) Then
         Throw New FileNotFoundException("The file does not exist.")
      End If   
      
      Me.filename = filename
      Dim txt As String = String.Empty
      Dim sr As StreamReader = Nothing
      Try
         sr = New StreamReader(filename)
         txt = sr.ReadToEnd()
      Finally
         If sr IsNot Nothing Then sr.Dispose() 
      End Try
      nWords = Regex.Matches(txt, pattern).Count
   End Sub
   
   Public ReadOnly Property FullName As String
      Get
         Return filename
      End Get   
   End Property
   
   Public ReadOnly Property Name As String
      Get
         Return Path.GetFileName(filename)
      End Get   
   End Property
   
   Public ReadOnly Property Count As Integer
      Get
         Return nWords
      End Get
   End Property
End Class

詳細については、 try / finallyパターンは、「お試しください.キャッチしてください.Finally ステートメント、try-finally、またはtry-finally ステートメントします。For more information about the try/finally pattern, see Try...Catch...Finally Statement, try-finally, or try-finally Statement.

IDisposable の実装Implementing IDisposable

実装する必要がありますIDisposable型が直接アンマネージ リソースを使用している場合のみです。You should implement IDisposable only if your type uses unmanaged resources directly. 型のコンシューマーが呼び出すことができます、IDisposable.Dispose実装のインスタンスが不要になったときにリソースを解放します。The consumers of your type can call your IDisposable.Dispose implementation to free resources when the instance is no longer needed. 呼び出しに失敗するケースを処理するDispose、いずれかから派生したクラスを使用する必要がありますSafeHandleオーバーライドする必要があるか、アンマネージ リソースをラップする、Object.Finalize参照型のメソッド。To handle cases in which they fail to call Dispose, you should either use a class derived from SafeHandle to wrap the unmanaged resources, or you should override the Object.Finalize method for a reference type. 使用するいずれの場合も、Dispose解放、リリース、またはアンマネージ リソースをリセットするなど、アンマネージ リソースを使用した後、必要なクリーンアップ処理をすべてを実行するメソッド。In either case, you use the Dispose method to perform whatever cleanup is necessary after using the unmanaged resources, such as freeing, releasing, or resetting the unmanaged resources.

重要

アンマネージ リソースを使用してするとき、またはを破棄する必要がありますサブクラスを与える可能性が、実装する必要があります基底クラスを定義する場合、IDisposable.Disposeメソッドの 2 番目のオーバー ロードを提供およびDispose次で説明したように、セクション。If you are defining a base class that uses unmanaged resources and that either has, or is likely to have, subclasses that should be disposed, you should implement the IDisposable.Dispose method and provide a second overload of Dispose, as discussed in the next section.

IDisposable と継承の階層IDisposable and the inheritance hierarchy

破棄可能なサブクラスを持つ基本クラスを実装する必要がありますIDisposable次のようにします。A base class with subclasses that should be disposable must implement IDisposable as follows. 実装するときに、このパターンを使用する必要がありますIDisposableはない任意の型でsealed(NotInheritable Visual Basic で)。You should use this pattern whenever you implement IDisposable on any type that isn't sealed (NotInheritable in Visual Basic).

  • 1 つのパブリック非仮想を指定する必要がありますDispose()メソッドと、保護された仮想Dispose(Boolean disposing)メソッド。It should provide one public, non-virtual Dispose() method and a protected virtual Dispose(Boolean disposing) method.

  • Dispose()メソッドを呼び出す必要がありますDispose(true)パフォーマンスの最終処理を抑制する必要があります。The Dispose() method must call Dispose(true) and should suppress finalization for performance.

  • 基本型はファイナライザーを含めることはできません。The base type should not include any finalizers.

次のコード フラグメントでは、基底クラスで dispose パターンを反映しています。The following code fragment reflects the dispose pattern for base classes. 種類をオーバーライドしないことを前提としていますが、Object.Finalizeメソッド。It assumes that your type does not override the Object.Finalize method.

using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.InteropServices;

class BaseClass : IDisposable
{
   // Flag: Has Dispose already been called?
   bool disposed = false;
   // Instantiate a SafeHandle instance.
   SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
   
   // Public implementation of Dispose pattern callable by consumers.
   public void Dispose()
   { 
      Dispose(true);
      GC.SuppressFinalize(this);           
   }
   
   // Protected implementation of Dispose pattern.
   protected virtual void Dispose(bool disposing)
   {
      if (disposed)
         return; 
      
      if (disposing) {
         handle.Dispose();
         // Free any other managed objects here.
         //
      }
      
      disposed = true;
   }
}
Imports Microsoft.Win32.SafeHandles
Imports System.Runtime.InteropServices

Class BaseClass : Implements IDisposable
   ' Flag: Has Dispose already been called?
   Dim disposed As Boolean = False
   ' Instantiate a SafeHandle instance.
   Dim handle As SafeHandle = New SafeFileHandle(IntPtr.Zero, True)

   ' Public implementation of Dispose pattern callable by consumers.
   Public Sub Dispose() _
              Implements IDisposable.Dispose
      Dispose(True)
      GC.SuppressFinalize(Me)           
   End Sub
   
   ' Protected implementation of Dispose pattern.
   Protected Overridable Sub Dispose(disposing As Boolean)
      If disposed Then Return
      
      If disposing Then
         handle.Dispose()
         ' Free any other managed objects here.
         '
      End If
      
      disposed = True
   End Sub
End Class

オーバーライドする場合、Object.Finalizeメソッド、クラスは、次のパターンを実装する必要があります。If you do override the Object.Finalize method, your class should implement the following pattern.

using System;

class BaseClass : IDisposable
{
   // Flag: Has Dispose already been called?
   bool disposed = false;
   
   // Public implementation of Dispose pattern callable by consumers.
   public void Dispose()
   { 
      Dispose(true);
      GC.SuppressFinalize(this);           
   }
   
   // Protected implementation of Dispose pattern.
   protected virtual void Dispose(bool disposing)
   {
      if (disposed)
         return; 
      
      if (disposing) {
         // Free any other managed objects here.
         //
      }
      
      // Free any unmanaged objects here.
      //
      disposed = true;
   }

   ~BaseClass()
   {
      Dispose(false);
   }
}
Class BaseClass : Implements IDisposable
   ' Flag: Has Dispose already been called?
   Dim disposed As Boolean = False
   
   ' Public implementation of Dispose pattern callable by consumers.
   Public Sub Dispose() _
              Implements IDisposable.Dispose
      Dispose(True)
      GC.SuppressFinalize(Me)           
   End Sub
   
   ' Protected implementation of Dispose pattern.
   Protected Overridable Sub Dispose(disposing As Boolean)
      If disposed Then Return
      
      If disposing Then
         ' Free any other managed objects here.
         '
      End If
      
      ' Free any unmanaged objects here.
      '
      disposed = True
   End Sub

   Protected Overrides Sub Finalize()
      Dispose(False)      
   End Sub
End Class

サブクラスは破棄可能なパターンを次のように実装する必要があります。Subclasses should implement the disposable pattern as follows:

  • これらは Dispose(Boolean) をオーバーライドし、基底クラスの Dispose(Boolean) の実装を呼び出す必要があります。They must override Dispose(Boolean) and call the base class Dispose(Boolean) implementation.

  • 必要な場合にはファイナライザーを提供します。They can provide a finalizer if needed. ファイナライザーは Dispose(false) を呼び出す必要があります。The finalizer must call Dispose(false).

派生クラスは実装しないこと自体に注意してください、IDisposableインターフェイスし、パラメーターなしに含めないでくださいDisposeメソッド。Note that derived classes do not themselves implement the IDisposable interface and do not include a parameterless Dispose method. 基底クラスのみをオーバーライドするDispose(Boolean)メソッド。They only override the base class Dispose(Boolean) method.

次のコード フラグメントでは、派生クラスで dispose パターンを反映しています。The following code fragment reflects the dispose pattern for derived classes. 種類をオーバーライドしないことを前提としていますが、Object.Finalizeメソッド。It assumes that your type does not override the Object.Finalize method.

using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.InteropServices;

class DerivedClass : BaseClass
{
   // Flag: Has Dispose already been called?
   bool disposed = false;
   // Instantiate a SafeHandle instance.
   SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);

   // Protected implementation of Dispose pattern.
   protected override void Dispose(bool disposing)
   {
      if (disposed)
         return; 
      
      if (disposing) {
         handle.Dispose();
         // Free any other managed objects here.
         //
      }
      
      // Free any unmanaged objects here.
      //

      disposed = true;
      // Call base class implementation.
      base.Dispose(disposing);
   }
}
Imports Microsoft.Win32.SafeHandles
Imports System.Runtime.InteropServices

Class DerivedClass : Inherits BaseClass 
   ' Flag: Has Dispose already been called?
   Dim disposed As Boolean = False
   ' Instantiate a SafeHandle instance.
   Dim handle As SafeHandle = New SafeFileHandle(IntPtr.Zero, True)

   ' Protected implementation of Dispose pattern.
   Protected Overrides Sub Dispose(disposing As Boolean)
      If disposed Then Return
      
      If disposing Then
         handle.Dispose()
         ' Free any other managed objects here.
         '
      End If
      
      ' Free any unmanaged objects here.
      '
      disposed = True
      
      ' Call base class implementation.
      MyBase.Dispose(disposing)
   End Sub
End Class

メソッド

Dispose() Dispose() Dispose() Dispose()

アンマネージ リソースの解放またはリセットに関連付けられているアプリケーション定義のタスクを実行します。Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

適用対象

こちらもご覧ください