从IResourceChangeListener刷新项目

时间:2014-01-02 03:27:44

标签: java eclipse

我已经实现IResourceChangeListener来跟踪在eclipse中对我的项目所做的更改。触发更改后,我会执行一些操作并使用以下代码刷新项目。执行此代码时,我得到异常(代码后面)。有没有办法检查某些东西 - 资源是否被锁定 - 并等待一段时间再进行刷新?

try {
    ResourcesPlugin.getWorkspace().getRoot().getProject(projectName)
        .refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
} catch (final CoreException ex) {
    ex.printStackTrace();
} catch (final Exception ex) {
    ex.printStackTrace();
}

org.eclipse.core.runtime.AssertionFailedException: assertion failed: The workspace tree is already locked
at org.eclipse.core.runtime.Assert.isTrue(Assert.java:110)
at org.eclipse.core.internal.resources.Workspace.setTreeLocked(Workspace.java:2416)
at org.eclipse.core.internal.events.NotificationManager.notify(NotificationManager.java:278)
at org.eclipse.core.internal.events.NotificationManager.handleEvent(NotificationManager.java:267)
at org.eclipse.core.internal.resources.Workspace.broadcastEvent(Workspace.java:390)
at org.eclipse.core.internal.resources.Resource.refreshLocal(Resource.java:1662)
at org.fastcode.util.SourceUtil.refreshProject(SourceUtil.java:4843)
at org.fastcode.util.FastCodeResourceChangeListener$1.onSuccess(FastCodeResourceChangeListener.java:198)
at org.fastcode.util.FastCodeResourceChangeListener$3.run(FastCodeResourceChangeListener.java:355)
at org.eclipse.ui.internal.UILockListener.doPendingWork(UILockListener.java:164)
at org.eclipse.ui.internal.UISynchronizer$3.run(UISynchronizer.java:158)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.jface.operation.ModalContext$ModalContextThread.block(ModalContext.java:173)
at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:388)
at org.eclipse.jface.wizard.WizardDialog.run(WizardDialog.java:1029)
at org.eclipse.ui.dialogs.WizardNewFolderMainPage.createNewFolder(WizardNewFolderMainPage.java:506)
at org.eclipse.ui.wizards.newresource.BasicNewFolderResourceWizard.performFinish(BasicNewFolderResourceWizard.java:92)
at org.eclipse.jface.wizard.WizardDialog.finishPressed(WizardDialog.java:827)
at org.eclipse.jface.wizard.WizardDialog.buttonPressed(WizardDialog.java:432)
at org.eclipse.jface.dialogs.Dialog$2.widgetSelected(Dialog.java:624)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
at org.eclipse.jface.window.Window.open(Window.java:801)
at org.eclipse.ui.internal.actions.NewWizardShortcutAction.run(NewWizardShortcutAction.java:135)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)

1 个答案:

答案 0 :(得分:0)

资源更改通知是由工作区的更改引起的;在收到通知时,您无法对工作区进行更改。如果你真的需要,可以安排一份工作(https://www.eclipse.org/articles/Article-Concurrency/jobs-api.html),但这听起来好像你正在做一些你不应该做的事。