eclipse 如何诊断无效的线程访问 SWTException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/511317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to diagnose an Invalid thread access SWTException?
提问by Caoilte
We're customizing an Eclipse RCP based tool for a client. They have trouble loading it on one of their computers (it works on others) and have provided the following error log.
我们正在为客户定制一个基于 Eclipse RCP 的工具。他们在其中一台计算机上加载它时遇到问题(它适用于其他计算机)并提供了以下错误日志。
!SESSION 2009-01-23 12:09:05.593 ----------------------------------------------- eclipse.buildId=unknown java.version=1.5.0_12 java.vendor=Sun Microsystems Inc. BootLoader constants: OS=win32, ARCH=x86, WS=win32, NL=en_GB Command-line arguments: -os win32 -ws win32 -arch x86
!ENTRY org.eclipse.osgi 4 0 2009-01-23 12:09:07.500 !MESSAGE Bundle com.yantra.yfc.rcp.desktop.ri not found.
!ENTRY org.eclipse.osgi 4 0 2009-01-23 12:09:11.906 !MESSAGE Application error !STACK 1 org.eclipse.swt.SWTException: Invalid thread access at org.eclipse.swt.SWT.error(SWT.java:3374) at org.eclipse.swt.SWT.error(SWT.java:3297) at org.eclipse.swt.SWT.error(SWT.java:3268) at org.eclipse.swt.widgets.Display.error(Display.java:978) at org.eclipse.swt.widgets.Display.checkDevice(Display.java:638) at org.eclipse.swt.graphics.Device.dispose(Device.java:261) at com.yantra.yfc.rcp.YRCApplication.run(YRCApplication.java:176) at org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:78) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:92) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:68) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:400) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177) 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.core.launcher.Main.invokeFramework(Main.java:334) at org.eclipse.core.launcher.Main.basicRun(Main.java:278) at org.eclipse.core.launcher.Main.run(Main.java:973) at org.eclipse.core.launcher.Main.main(Main.java:948)
!SESSION 2009-01-23 12:09:05.593 -------------------------------------- --------- eclipse.buildId=unknown java.version=1.5.0_12 java.vendor=Sun Microsystems Inc. BootLoader 常量:OS=win32, ARCH=x86, WS=win32, NL=en_GB 命令行参数:-os win32 -ws win32 -arch x86
!ENTRY org.eclipse.osgi 4 0 2009-01-23 12:09:07.500 !MESSAGE Bundle com.yantra.yfc.rcp.desktop.ri 未找到。
!ENTRY org.eclipse.osgi 4 0 2009-01-23 12:09:11.906 !MESSAGE Application error !STACK 1 org.eclipse.swt.SWTException: org.eclipse.swt.SWT.error(SWT. java:3374) 在 org.eclipse.swt.SWT.error(SWT.java:3297) 在 org.eclipse.swt.SWT.error(SWT.java:3268) 在 org.eclipse.swt.widgets.Display.error (Display.java:978) at org.eclipse.swt.widgets.Display.checkDevice(Display.java:638) at org.eclipse.swt.graphics.Device.dispose(Device.java:261) at com.yantra。 yfc.rcp.YRCApplication.run(YRCApplication.java:176) 在 org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:78) 在 org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher .runApplication(EclipseAppLauncher.java:92) 在 org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher。start(EclipseAppLauncher.java:68) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:400) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在 java.lang.reflect.Method.invoke(Unknown Source)在 org.eclipse.core.launcher.Main.invokeFramework(Main.java:334) 在 org.eclipse.core.launcher.Main.basicRun(Main.java:278) 在 org.eclipse.core.launcher.Main.run (Main.java:973) 在 org.eclipse.core.launcher.Main.main(Main.java:948)core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177) 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.core.launcher.Main.invokeFramework(Main.java:334) at org.eclipse.core.launcher.Main.basicRun(Main. java:278) 在 org.eclipse.core.launcher.Main.run(Main.java:973) 在 org.eclipse.core.launcher.Main.main(Main.java:948)core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177) 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.core.launcher.Main.invokeFramework(Main.java:334) at org.eclipse.core.launcher.Main.basicRun(Main. java:278) 在 org.eclipse.core.launcher.Main.run(Main.java:973) 在 org.eclipse.core.launcher.Main.main(Main.java:948)invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.core.launcher.Main.invokeFramework(Main.java:334) at org.eclipse.core.launcher.Main.basicRun (Main.java:278) 在 org.eclipse.core.launcher.Main.run(Main.java:973) 在 org.eclipse.core.launcher.Main.main(Main.java:948)invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.eclipse.core.launcher.Main.invokeFramework(Main.java:334) at org.eclipse.core.launcher.Main.basicRun (Main.java:278) 在 org.eclipse.core.launcher.Main.run(Main.java:973) 在 org.eclipse.core.launcher.Main.main(Main.java:948)
I have googled the exception but this seems to happen mostly when people try to develop applications using different threads. Since we don't see this problem on any other computer I'm at a loss as to what might be causing it.
我用谷歌搜索了异常,但这似乎主要发生在人们尝试使用不同线程开发应用程序时。由于我们在任何其他计算机上都没有看到此问题,因此我不知道可能是什么原因造成的。
It must be a configuration problem on the machine in question, as the code is from the vendor (so presumably well tested) and works on every other machine we've tested it on.
这一定是有问题的机器上的配置问题,因为代码来自供应商(因此可能经过良好测试)并且适用于我们测试过的所有其他机器。
Does anyone have any suggestions about what might be behind the problem for that computer? Or suggestions about lines of investigation which might reveal the issue?
有没有人对那台计算机的问题背后可能有什么建议?或者关于可能揭示问题的调查路线的建议?
回答by zvikico
There's only one UI thread in Eclipse. In a nutshell, the rules are:
Eclipse 中只有一个 UI 线程。简而言之,规则是:
- If you got called as part of a UI operation (e.g. event handler, view initialization) you are in the UI thread.
- All other operations that invoke a UI (e.g. a job which needs to show a dialog or send information to a view which modifies a widget) - need to sync with the UI thread.
- 如果您作为 UI 操作(例如事件处理程序、视图初始化)的一部分被调用,则您处于 UI 线程中。
- 调用 UI 的所有其他操作(例如,需要显示对话框或将信息发送到修改小部件的视图的作业) - 需要与 UI 线程同步。
This is basically done like this:
这基本上是这样完成的:
Display.getDefault().syncExec( new Runnable() { public void run() { } });
Your code goes in the run method. You may also use the asyncExec
method to continue without waiting for the UI to finish.
您的代码在 run 方法中。您也可以使用该asyncExec
方法继续,而无需等待 UI 完成。
Try using the snippet above to wrap the problematic code.
尝试使用上面的代码段来包装有问题的代码。
EDIT: Ending bracket for Runnable() was missing in the snippet . After adding snippet works fine.
编辑:片段中缺少 Runnable() 的结束括号。添加片段后工作正常。
回答by Mario Ortegón
It seems to me that an Exception is thrown in the application thread, that happens only in some machines. Probably there is code in the RCP application to display the exception on the GUI with some dialog box, but this is done in the wrong thread. That would explain why it happens only on some machines. It would also explain why the problem went undetected.... it probably never happens in the dev's computers so they never bothered to check that the UI Access is done using the right thread. I had a similar problem once.
在我看来,在应用程序线程中抛出了一个异常,这只发生在某些机器上。RCP 应用程序中可能有代码可以在带有某些对话框的 GUI 上显示异常,但这是在错误的线程中完成的。这将解释为什么它只发生在某些机器上。它还可以解释为什么问题没有被发现......它可能永远不会发生在开发人员的计算机中,所以他们从不费心检查 UI 访问是否使用正确的线程完成。我曾经遇到过类似的问题。
My suggestion would be to take a close look to:
我的建议是仔细研究:
com.yantra.yfc.rcp.YRCApplication.run(YRCApplication.java:176)
com.yantra.yfc.rcp.YRCApplication.run(YRCApplication.java:176)
As the code is compiled with line numbers, you can attach a debugger to this line even if you have no source code, and try to see what happens. I am pretty sure that when you hit this breakpoint on the problematic machine, an Exception will be thrown. That will be your "Real" exception.
由于代码是用行号编译的,即使您没有源代码,您也可以将调试器附加到这一行,然后尝试看看会发生什么。我很确定当你在有问题的机器上遇到这个断点时,会抛出一个异常。那将是您的“真正”例外。
回答by Fabian Steeg
You can also find the answer in The Official Eclipse FAQs, in particular: Why do I get an invalid thread access exception?
您还可以在The Official Eclipse FAQs 中找到答案,特别是:Why do I get an invalid thread access exception?
回答by Boris Bokowski
If looking at the source code for YRCApplication line 176 does not help (why is it calling Display.dispose() when it is about to start?), I would attach an external debugger to the process running on that particular machine. Have a look at http://wiki.eclipse.org/Ninja#How_to_run_Eclipse_so_that_you_can_attach_an_external_debuggerfor instructions; if you try to remote-debug the situation from a remote machine you'd have to ensure that firewalls etc. are not blocking the TCP connection used by the debugger.
如果查看 YRCApplication 第 176 行的源代码没有帮助(为什么它在即将启动时调用 Display.dispose()?),我会将外部调试器附加到该特定机器上运行的进程。查看http://wiki.eclipse.org/Ninja#How_to_run_Eclipse_so_that_you_can_attach_an_external_debugger以获取说明;如果您尝试从远程机器远程调试情况,您必须确保防火墙等不会阻止调试器使用的 TCP 连接。