Invalid Thread Access

牧云@^-^@ 提交于 2019-12-11 19:46:15

问题


The create View Control Class

public void createPartControl(Composite parent) {
        //viewer = new TableViewer(parent, SWT.MULTI| SWT.V_SCROLL);
        //viewer.setContentProvider(new ViewContentProvider());
        //viewer.setLabelProvider(new ViewLabelProvider());
        // Provide the input to the ContentProvider
        //viewer.setInput(new String[] {"One", "Two", "Three"});
        //Display display=new Display();
        parent.getShell().setText("A dialog box with no buttons at all press 'ESC' to close");
        final Shell shell = new Shell(Display.getCurrent());
        new Thread(new Runnable()
        {
            public void run()
            {
                while(true)
                {
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch(Exception e)
                    {

                    }
                    Display.getDefault().asyncExec(new Runnable()
                    {
                        public void run()
                        {
                            shell.addPaintListener(new PaintListener() {
                                @Override
                                public void paintControl(PaintEvent event) {
                                    Rectangle rect = shell.getClientArea();
                                    event.gc.drawOval(0, 0, rect.width - 1, rect.height - 1);
                                }
                            });
                            Rectangle clientArea = shell.getClientArea();
                            shell.setBounds(clientArea.x + 10, clientArea.y + 10, 200, 200);
                            shell.open ();
                            while (!shell.isDisposed()) 
                            {
                                if (!Display.getCurrent().readAndDispatch()) 
                                    Display.getCurrent().sleep();
                            }
                            Display.getCurrent().dispose();

                        }
                    });
                }
            }

        }).start();
}

Can anyone explain how threading works in SWT graphics . It says invalid thread access . According to me there is a thread running and i am creating another thread, which is definitely wrong.So what is correct method. In which line default thread is being created.
It also gives a widget disposed error. Why?

The following code has been created from the default view plugin and here
My aim is to create diagrams in my view itself not in a separate dialog box. I have also attached the log file.

!ENTRY org.eclipse.osgi 4 0 2015-02-23 12:47:18.520
!MESSAGE Application error
!STACK 1
org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4441)
    at org.eclipse.swt.SWT.error(SWT.java:4356)
    at org.eclipse.swt.SWT.error(SWT.java:4327)
    at org.eclipse.swt.widgets.Display.error(Display.java:1258)
    at org.eclipse.swt.widgets.Display.checkDevice(Display.java:764)
    at org.eclipse.swt.widgets.Display.removeFilter(Display.java:4065)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.cleanUp(PartRenderingEngine.java:1241)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$5(PartRenderingEngine.java:1237)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1166)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1032)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:148)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:636)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:579)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
    at swtdiagrams.Application.start(Application.java:20)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:382)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:236)
    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:648)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1438)

!ENTRY org.eclipse.ui.workbench 4 2 2015-02-23 12:47:18.535
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.ui.workbench".
!STACK 0
org.eclipse.swt.SWTException: Widget is disposed
    at org.eclipse.swt.SWT.error(SWT.java:4441)
    at org.eclipse.swt.SWT.error(SWT.java:4356)
    at org.eclipse.swt.SWT.error(SWT.java:4327)
    at org.eclipse.swt.widgets.Display.error(Display.java:1258)
    at org.eclipse.swt.widgets.Display.checkDevice(Display.java:745)
    at org.eclipse.swt.widgets.Display.getActiveShell(Display.java:1469)
    at org.eclipse.ui.internal.services.WorkbenchSourceProvider.getActiveWindow(WorkbenchSourceProvider.java:446)
    at org.eclipse.ui.internal.services.WorkbenchSourceProvider.updateActivePart(WorkbenchSourceProvider.java:478)
    at org.eclipse.ui.internal.services.WorkbenchSourceProvider.checkActivePart(WorkbenchSourceProvider.java:316)
    at org.eclipse.ui.internal.services.WorkbenchSourceProvider.checkActivePart(WorkbenchSourceProvider.java:311)
    at org.eclipse.ui.internal.services.WorkbenchSourceProvider$1.partDeactivated(WorkbenchSourceProvider.java:256)
    at org.eclipse.ui.internal.PartService$4.run(PartService.java:123)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.ui.internal.PartService.partDeactivated(PartService.java:120)
    at org.eclipse.ui.internal.WorkbenchPage$17.run(WorkbenchPage.java:4823)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.ui.internal.WorkbenchPage.firePartDeactivated(WorkbenchPage.java:4820)
    at org.eclipse.ui.internal.WorkbenchPage.access$20(WorkbenchPage.java:4813)
    at org.eclipse.ui.internal.WorkbenchPage$E4PartListener.partDeactivated(WorkbenchPage.java:214)
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl$4.run(PartServiceImpl.java:250)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.firePartDeactivated(PartServiceImpl.java:247)
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.activate(PartServiceImpl.java:619)
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.setPart(PartServiceImpl.java:205)
    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.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
    at org.eclipse.e4.core.internal.contexts.ContextObjectSupplier$ContextInjectionListener.update(ContextObjectSupplier.java:88)
    at org.eclipse.e4.core.internal.contexts.TrackableComputationExt.update(TrackableComputationExt.java:107)
    at org.eclipse.e4.core.internal.contexts.EclipseContext.processScheduled(EclipseContext.java:338)
    at org.eclipse.e4.core.internal.contexts.EclipseContext.set(EclipseContext.java:352)
    at org.eclipse.e4.core.internal.contexts.EclipseContext.dispose(EclipseContext.java:196)
    at org.eclipse.e4.core.internal.contexts.EclipseContext.dispose(EclipseContext.java:160)
    at org.eclipse.e4.core.internal.contexts.EclipseContext.dispose(EclipseContext.java:160)
    at org.eclipse.e4.core.internal.contexts.EclipseContext.dispose(EclipseContext.java:160)
    at org.eclipse.e4.core.internal.contexts.EclipseContext.dispose(EclipseContext.java:160)
    at org.eclipse.e4.core.internal.contexts.osgi.EclipseContextOSGi.dispose(EclipseContextOSGi.java:103)
    at org.eclipse.e4.core.internal.contexts.osgi.EclipseContextOSGi.bundleChanged(EclipseContextOSGi.java:134)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.dispatchEvent(BundleContextImpl.java:902)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)
    at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEventPrivileged(EquinoxEventPublisher.java:165)
    at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEvent(EquinoxEventPublisher.java:75)
    at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEvent(EquinoxEventPublisher.java:67)
    at org.eclipse.osgi.internal.framework.EquinoxContainerAdaptor.publishModuleEvent(EquinoxContainerAdaptor.java:102)
    at org.eclipse.osgi.container.Module.publishEvent(Module.java:466)
    at org.eclipse.osgi.container.Module.doStop(Module.java:624)
    at org.eclipse.osgi.container.Module.stop(Module.java:488)
    at org.eclipse.osgi.container.SystemModule.stop(SystemModule.java:186)
    at org.eclipse.osgi.internal.framework.EquinoxBundle$SystemBundle$EquinoxSystemModule$1.run(EquinoxBundle.java:160)
    at java.lang.Thread.run(Unknown Source)

I have studied a lot of Stackoverflow pages on the same issues but none of issues were similar to me(They never wanted to create diagrams on view). Whatever code you see is the result of all the SO suggestions. Many didnot even have answers just comments.
Please help.

EDIT After the edits told by Greg I am getting .

Is there anyway i can print it on the view(by view i mean the whitespace and not next to it.) Thanks.


回答1:


You are calling dispose() on the Display object - never do that in Eclipse plugins.

If you want to draw in the view area itself just use a Canvas control, something like:

public void createPartControl(Composite parent)
{
  // Create control

  final Canvas canvas = new Canvas(parent, SWT.NONE);

  // Set up a single paint listener

  canvas.addPaintListener(... paint the canvas );

  // Schedule a redraw after 1000 milliseconds

  Display.getCurrent().timerExec(1000, new Runnable() {
     public void run()
     {
       if (!canvas.isDisposed())
        {
          canvas.redraw();

          Display.getCurrent().timerExec(1000, this);
        }
     }
  });     
}


来源:https://stackoverflow.com/questions/28671415/invalid-thread-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!