Directory monitoring stops the system from responding to interactions

Asked

Viewed 41 times

0

I have an application that monitors a certain folder and checks if there is the creation of a new file if a new file is created triggering several processes on the system.

But when active monitoring seems it locks various processes and does not allow me almost any interaction with my system.

public void Monitoramento(){

    Path pastaOrigem;
    try {
        pastaOrigem = Files.createDirectories(Paths.get("C:\\monitoramento"));


    watcher = FileSystems.getDefault().newWatchService();


    pastaOrigem.register(watcher, ENTRY_CREATE);

    while (true) {

        WatchKey wk = null;
        try {

            wk = watcher.take();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        for (WatchEvent<?> event : wk.pollEvents()) {

            if (event.kind() == OVERFLOW) {
                continue;
            }

            WatchEvent<Path> ev = (WatchEvent<Path>) event;
            Path nomeArquivo = ev.context();
            Path arquivoOrigem = pastaOrigem.resolve(nomeArquivo);

            String arquivo = nomeArquivo.toString();

            if (arquivo.substring(0, 3).equals("vn")) {


               novoEvento ne = new novoEvento();


               ne.leituraArquivo(arquivoOrigem);

            }

        }

        if (!wk.reset()) {
            break;
        }
    }

    } catch (IOException ex) {
        Logger.getLogger(TelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);

    } catch (Exception ex){
        ex.printStackTrace();
    }
}
  • has a similar question but it is not the same subject.

  • It would be a good idea to put some information about the host system.

  • Dude, checking with my code I made here, I noticed only one difference. There at the end you check if the key has been reset and a break. I check if the key has been reset, otherwise I do a Keys.remove(wk), then check if wk.isEmpty, then break.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.