Create a background task in IntelliJ plugin

后端 未结 2 997
难免孤独
难免孤独 2021-02-05 13:51

I\'m developing an IntelliJ-idea plugin and want to run code in background task (visible in the background tasks dialog and in another thread than the UI).

I found the f

2条回答
  •  星月不相逢
    2021-02-05 14:27

    I have found a better way to run the process as background task where you can update the progress bar percentage and text

    ProgressManager.getInstance().run(new Task.Backgroundable(project, "Title"){
            public void run(@NotNull ProgressIndicator progressIndicator) {
    
                // start your process
    
                // Set the progress bar percentage and text
                progressIndicator.setFraction(0.10);
                progressIndicator.setText("90% to finish");
    
    
                // 50% done
                progressIndicator.setFraction(0.50);
                progressIndicator.setText("50% to finish");
    
    
                // Finished
                progressIndicator.setFraction(1.0);
                progressIndicator.setText("finished");
    
            }});
    

    If you need to read some data from another thread you should use

    AccessToken token = null;
    try {
       token = ApplicationManager.getApplication().acquireReadActionLock();
                        //do what you need
    } finally {
       token.finish();
    }
    

提交回复
热议问题