IntelliJ Background task indicator doesn't disappear after task finishes
Hello, I am building a plugin that downloads files from a remote server, I use Task. Backgroundable and ProgressManager.getInstance().run() so we can have several parallel downloads (download takes 1-20 seconds). When I start multiple background tasks I can see a progress bar and task count at the bottom, but sometimes the progress bar doesn't disappear for a random task after it's completed (doesn't dissapear for one task only). I use distinguish names for each task.
An example of a progress bar that never disappears: 
My code looks following:
ProgressManager.getInstance().run(new Task.Backgroundable(systemLogToolWindow.getProject(), "Downloading " + thisNode.name) {
public void run(@NotNull ProgressIndicator progressIndicator) {
// start your process
// Here goes my downloading steps, where I update some UI icons on when start and fginish downloading
// Finished
progressIndicator.setFraction(1.0);
progressIndicator.setText("finished");
progressIndicator.cancel();
}
});
1) Please be so kind as to help in solving the issue
2) Also is there any API to get a list of all running background task with the ability to stop any of it.
Thank you in advance
Please sign in to leave a comment.
Why did you put
?
Please always post full code, not just snippets.
Yann,
Thank you for your answer.
I use cancel() method at the end to ensure that the status bar is removed (I might not fully understand API, so please correct me if I am wrong).
The full code would look like:
where socket.getSysLogFile:
public void getSysLogFile(String fileName, String localPathToFile) {
OutputStream output = null;
LogNotification notification = new LogNotification("Error", "Fail to Download File: " + fileName, NotificationType.ERROR);
try {
output = logAppService.getSysLogFile(socketId, fileName);
} catch (java.lang.OutOfMemoryError e) {
LOG.info(e.getMessage());
IOUtils.closeQuietly(output);
notification.notify(project);
return;
}
// saving to file
ByteArrayInputStream inputStream = null;
Scanner sc = null;
PrintWriter pw = null;
try {
inputStream = new ByteArrayInputStream(((ByteArrayOutputStream) output).toByteArray());
sc = new Scanner(inputStream, "UTF-8");
pw = new PrintWriter(new FileWriter(localPathToFile));
while (sc.hasNextLine()) {
String logLine = sc.nextLine();
pw.write(logLine);
pw.println();
}
} catch (IOException e) {
LOG.info(e.getMessage());
notification.notify(project);
} finally {
IOUtils.closeQuietly(sc);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(pw);
}
}
cancel() call is used to interrupt running task, not to finish/notify finish. Please remove it.
Got it, will remove. Do you know why sometimes task indicator doesn't disappear after task finishes? Also is there any API to get a list of all running background tasks?
It shouldn't and I hope it will not re-occur after you remove cancel() call.
Thank you. I fix my issues.
The problem I had was caused by an an infinite loop down my task execution :)
Yann, one more question about tasksks:
is there any API to get a list of all running background tasks?
com.intellij.openapi.wm.ex.StatusBarEx#getBackgroundProcesses
Yann, you are awesome! I appreciate your help.