我在我的程序中进行Win API调用,其中一些假设需要释放资源。我可以将这种释放放入终结器并确保它会被调用吗?
以下是我现在在静态方法中的表现:
class KeyboardListener
{
static KeyboardListener() {
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
Install(keyboardHook);
}
static void OnProcessExit(object sender, EventArgs e)
{
Uninstall();
}
static void Install(KeyboardHookDelegate proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
keyboardHookID = SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
static void Uninstall()
{
UnhookWindowsHookEx(keyboardHookID);
}
现在我想将其重写为非静态并希望决定将Uninstall
调用放在何处。