在IntelliJ插件中创建后台任务 [英] Create a background task in IntelliJ plugin

查看:2190
本文介绍了在IntelliJ插件中创建后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个IntelliJ-idea插件,并希望在后台任务中运行代码(在后台任务对话框中和在UI之外的另一个线程中可见)。

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).

我找到了以下帮助程序类并通过传递尝试一个Runnable对象并实现其run方法,但它仍然阻止了UI,当我尝试自己进行线程处理时出现以下错误

I found the following Helper class and tried it by passing a Runnable object and implement its run method but it still blocking the UI and when I tried to do the threading myself i got the following error

 Read access is allowed from event dispatch thread or inside read-action only (see com.intellij.openapi.application.Application.runReadAction())
     Details: Current thread: Thread[Thread-69 [WriteAccessToken],6,Idea Thread Group] 532224832
     Our dispatch thread:Thread[AWT-EventQueue-1 12.1.4#IU-129.713, eap:false,6,Idea Thread Group] 324031064
     SystemEventQueueThread: Thread[AWT-EventQueue-1 12.1.4#IU-129.713, eap:false,6,Idea Thread Group] 324031064


推荐答案

我找到了一个更好的方法来运行流程作为后台任务和你更新进度条百分比和文本

I have found a better way which run the process as background task and you 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 read some data from other thread you should use

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

这篇关于在IntelliJ插件中创建后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆