gpt4 book ai didi

java - SWT异常 : Invalid thread access

转载 作者:行者123 更新时间:2023-12-01 22:38:57 30 4
gpt4 key购买 nike

我正在使用一个用于创建 java 项目的向导,如果我第一次在空工作区中运行它,则会收到无效的线程访问异常。我尝试实现类似于 JavaProjectWizard 的向导,但我不需要第二页,因此我尝试从第一页执行完成并提前初始化第二页:

import org.eclipse.jdt.ui.wizards.NewJavaProjectWizardPageTwo;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;

public class SomeNewWizard
extends Wizard
implements INewWizard {

private SomeWizardPageTwo javaWizardPageTwo;

@Override
public void addPages() {
if (javaWizardPageTwo == null)
someWizardPageTwo = new SomeWizardPageTwo(newSeeAppWizardPageOne);
}

@Override
public boolean performFinish() {
/*line 109*/someWizardPageTwo .createProvisonalProject();
final IWorkspaceRunnable op = new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor)
throws CoreException, OperationCanceledException {
try {
someWizardPageTwo.performFinish(new SubProgressMonitor(monitor, 1));
}
catch (InterruptedException e) {
throw new OperationCanceledException(e.getMessage());
}
finally {
monitor.done();
}
}
};
try {
rule = null;
Job job = Job.getJobManager().currentJob();
if (job != null)
rule = job.getRule();
IRunnableWithProgress runnable = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException {
try {
JavaCore.run(op, rule, monitor);
}
catch (OperationCanceledException e) {
throw new InterruptedException(e.getMessage());
}
catch (CoreException e) {
throw new InvocationTargetException(e);
}
}
};
getContainer().run(true, true, runnable);
}
catch (InvocationTargetException e) {
handleFinishException(getShell(), e);
return false;
}
catch (InterruptedException e) {
return false;
}
return true;
}

public class SomeWizardPageTwo
extends NewJavaProjectWizardPageTwo
{

private NewJavaProjectWizardPageOne mainPage;


public SomeWizardPageTwo(NewJavaProjectWizardPageOne mainPage)
{
super(mainPage);
this.mainPage = mainPage;
}


@Override
public IProject createProvisonalProject()
{
return super.createProvisonalProject();
}


@Override
protected IWizardContainer getContainer()
{
if (mainPage == null)
return null;
return mainPage.getWizard().getContainer();
}
}

可以找到堆栈跟踪 here .

问题的根源似乎是 ImageDescriptorRegistry 是从错误的线程创建的,因为相应的 IRunnableWithProgress 运行 forked 。但我想知道为什么它适用于普通的 JavaProjectWizard 呢?主要问题是:如何让它为我的向导工作?

最佳答案

getContainer().run(true, true, runnable); 导致 runnable 在单独的线程中执行。因此,第一个参数 fork 负责。对 javaWizardPageTwo.performFinish() 的调用尝试访问 UI 线程并导致无效线程访问异常。

如果将fork参数设置为false,代码将在当前线程上执行。

为什么不直接调用javaWizardPageTwo.performFinish()

关于java - SWT异常 : Invalid thread access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26481306/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com