无效的线程访问 [英] Invalid Thread Access

查看:81
本文介绍了无效的线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建视图控件类

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();
}

谁能解释线程在SWT图形中是如何工作的。它说无效的线程访问。据我说,有一个线程正在运行,而我正在创建另一个线程,这肯定是错误的。那么什么是正确的方法。在哪一行中创建默认线程。

它还给出了窗口小部件处理错误。为什么?

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?

以下代码是通过默认视图插件和
此处
< br>
我的目的是在视图本身中而不是在单独的对话框中创建图。
我还附加了日志文件。

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)

我研究了很多关于相同问题的Stackoverflow页面,但是没有一个问题与我(他们从不想在视图上创建图)。无论您看到什么代码,都是所有SO建议的结果。许多人甚至连评论都没有答案。


请帮忙。

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
Greg告诉编辑后,我得到了。

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.

推荐答案

Display 对象上调用 dispose()-绝对不要在Eclipse插件中这样做。

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

如果要绘制视图区域本身,只需使用 Canvas 控件,例如:

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);
        }
     }
  });     
}

这篇关于无效的线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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