我正在开发一个 IntelliJ-idea 插件,并希望在后台任务中运行代码(在后台任务对话框和 UI 之外的另一个线程中可见)。
我发现了以下内容助手类 https://github.com/inmite/android-selector-chapek/blob/master/src/utils/RunnableHelper.java并通过传递 Runnable 对象并实现其 run 方法进行尝试,但它仍然阻塞 UI,当我尝试自己执行线程时,出现以下错误
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
我找到了一种更好的方法来将进程作为后台任务运行,您可以在其中更新进度条百分比和文本
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");
}});
如果您需要从另一个线程读取一些数据,您应该使用
AccessToken token = null;
try {
token = ApplicationManager.getApplication().acquireReadActionLock();
//do what you need
} finally {
token.finish();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)