Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Reworked Shutdown of DeviceHookThread #5

Open
Felix-CodingClimber opened this issue May 19, 2020 · 0 comments
Open

Reworked Shutdown of DeviceHookThread #5

Felix-CodingClimber opened this issue May 19, 2020 · 0 comments

Comments

@Felix-CodingClimber
Copy link

Felix-CodingClimber commented May 19, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant