I’m trying to terminate the JNA call to WaitForSignleObject() with Timer that interrupts the current thread:
final Thread thread = Thread.currentThread();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
thread.interrupt();
}
}, 3000);
try {
Kernel32.INSTANCE.WaitForSingleObject(processInfo.hProcess, Kernel32.INFINITE);
...
} catch (InterruptedException e) {
}
The problem is that TimerTask.run() is not called after 3 seconds have passed as expected, it’s called only after WaitForSingleObject() exits itself. What am I doing wrong?
Thanks!
Thread interruption mechanism is a Java-specific feature, so it’s no surprise that native code doesn’t respect it.
If all you need is a timeout, you can use the second argument of
WaitForSingleObject()instead. If you need more complex logic, you can create an event to notify waiting thread about interruption, and useWaitForMultipleObjects()on that event and yourhProcess.