Home > OS >  Is it possible to interrupt a runnable called by ApplicationManager.getApplication().executeOnPooled
Is it possible to interrupt a runnable called by ApplicationManager.getApplication().executeOnPooled

Time:11-01

I am currently working on developing an IDEA extension that embeds JCEF for frontend interaction. Once the user clicks on a button, the implemented onQuery method in MessageRouterHandler would be executed, and a time-consuming task would be performed.

The time-consuming task is implemented in a method (asynchronous on a thread other than the main thread in case the UI to be frozen) and is wrapped in a form like

private void task(CefBrowser browser, CefQueryCallback callback) {
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        ...DETAILED IMPLEMENTATION FOR THE TASK...
    });
}

The runnable only contains the operation related to file reading (selected by the user clicking on another button on the JCEF webview) and has nothing to do with the IDE interface so I did not add runWriteAction or runReadAction here.

Since the task might take a longer time, I would like to add a button for the user's cancelling order. However, I am quite new to extension development, and could anyone help me to figure out how to implement the cancel function? Great great many thanks for any suggestions.

CodePudding user response:

A time consuming task should be performed under a progress indicator, see com.intellij.openapi.progress.ProgressManager. During the task you can then call com.intellij.openapi.progress.ProgressManager#checkCanceled to see if the task has been canceled by the user. This call is supposed to be cheap, so you can call it often to make sure your process responds promptly to the users' request for cancellation.

  • Related