Simplified-JNA icon indicating copy to clipboard operation
Simplified-JNA copied to clipboard

Reworked Shutdown of DeviceHookThread

Open Felix-CodingClimber opened this issue 5 years ago • 0 comments

A small change I made to the code to nicely shutdown the DeviceHookThread (With the current version it stays running even if you call unhook()) Maybe it helps someone :))

DeviceHookThreads add new private field:

// Native thread id private volatile int nativeThreadId;

DeviceHookThreads add to run() method:

@Override public void run() { WinDef.HMODULE handle = Kernel32.INSTANCE.GetModuleHandle(null); this.hhk = User32.INSTANCE.SetWindowsHookEx(hookType, eventReceiver, handle, 0); nativeThreadId = Kernel32.INSTANCE.GetCurrentThreadId(); int result; while ((result = getMessage()) != 0) { if (result == -1) { onFail(); break; } else { dispatchEvent(); } } unhook(); }

DeviceHookThreads add new exit() method:

// Stops the thread and removes the hook public void exit() { User32.INSTANCE.PostThreadMessage(nativeThreadId, WinUser.WM_QUIT, null, null); }

DeviceHookManager change unhook() method:

public void unhook(H eventReceiver) { hooks.get(eventReceiver).exit(); }

All the best Felix

Felix-CodingClimber avatar May 19 '20 15:05 Felix-CodingClimber