本文整理匯總了C#中System.GC.SuppressFinalize方法的典型用法代碼示例。如果您正苦於以下問題:C# GC.SuppressFinalize方法的具體用法?C# GC.SuppressFinalize怎麽用?C# GC.SuppressFinalize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.GC
的用法示例。
在下文中一共展示了GC.SuppressFinalize方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GetStdHandle
//引入命名空間
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class ConsoleMonitor : IDisposable
{
const int STD_INPUT_HANDLE = -10;
const int STD_OUTPUT_HANDLE = -11;
const int STD_ERROR_HANDLE = -12;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteConsole(IntPtr hConsoleOutput, string lpBuffer,
uint nNumberOfCharsToWrite, out uint lpNumberOfCharsWritten,
IntPtr lpReserved);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr handle);
private bool disposed = false;
private IntPtr handle;
private Component component;
public ConsoleMonitor()
{
handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle == IntPtr.Zero)
throw new InvalidOperationException("A console handle is not available.");
component = new Component();
string output = "The ConsoleMonitor class constructor.\n";
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
}
// The destructor calls Object.Finalize.
~ConsoleMonitor()
{
if (handle != IntPtr.Zero) {
string output = "The ConsoleMonitor finalizer.\n";
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
}
else {
Console.Error.WriteLine("Object finalization.");
}
// Call Dispose with disposing = false.
Dispose(false);
}
public void Write()
{
string output = "The Write method.\n";
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
}
public void Dispose()
{
string output = "The Dispose method.\n";
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
string output = String.Format("The Dispose({0}) method.\n", disposing);
uint written = 0;
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
// Execute if resources have not already been disposed.
if (! disposed) {
// If the call is from Dispose, free managed resources.
if (disposing) {
Console.Error.WriteLine("Disposing of managed resources.");
if (component != null)
component.Dispose();
}
// Free unmanaged resources.
output = "Disposing of unmanaged resources.";
WriteConsole(handle, output, (uint) output.Length, out written, IntPtr.Zero);
if (handle != IntPtr.Zero) {
if (! CloseHandle(handle))
Console.Error.WriteLine("Handle cannot be closed.");
}
}
disposed = true;
}
}
public class Example
{
public static void Main()
{
Console.WriteLine("ConsoleMonitor instance....");
ConsoleMonitor monitor = new ConsoleMonitor();
monitor.Write();
monitor.Dispose();
}
}
輸出:
ConsoleMonitor instance.... The ConsoleMonitor class constructor. The Write method. The ConsoleMonitor finalizer. The Dispose(False) method. Disposing of unmanaged resources. If the monitor.Dispose method is called, the example displays the following output: ConsoleMonitor instance.... The ConsoleMonitor class constructor. The Write method. The Dispose method. The Dispose(True) method. Disposing of managed resources. Disposing of unmanaged resources.