I'm trying to stop my thread where WatchService
is working. But how to do that? My WatchService
waiting for new update in folder there:
key = watchService.take();
I start my Thread there:
private void startButtonActionPerformed(ActionEvent e) {
stateLabel.setText("monitoring...");
try {
watchService = FileSystems.getDefault().newWatchService();
} catch (IOException e1) {
e1.printStackTrace();
}
watchThread = new WatchThread(watchService, key);
Thread t = new Thread(watchThread);
t.start();
}
My trying to stop:
private void stopButtonActionPerformed(ActionEvent e) {
try {
if (watchService!=null) {
key.cancel();
watchService.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
When I try to execute the stop, I get NullPointerException
in key.
When I'm just closing watchService with watchService.close()
, I get another exception: ClosedWatchServiceException
.
How to close a WatchService
without any exceptions?
Sorry for my bad english..