Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Advanced Scripting Environment (EASE) » Javascript EASE script error : Invalid thread access(I want to automate some UI action with Javascript, so the code should run in the UI thread)
Javascript EASE script error : Invalid thread access [message #1860818] Mon, 04 September 2023 17:20 Go to next message
Andreas Rückert is currently offline Andreas RückertFriend
Messages: 2
Registered: September 2023
Junior Member
Hello!

I want to automate some UI action with Javascript. As a start I wanted to select the entire source code and indent the code properly.

So I wrote some code, but I get the error:

======
org.eclipse.ease.ScriptExecutionException: JavaError: Invalid thread access
at hello.js,workspace://Andy_Refactorer/scripts/hello.js

Java Stacktrace:
org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:4918)
at org.eclipse.swt.SWT.error(SWT.java:4833)
at org.eclipse.swt.SWT.error(SWT.java:4804)
at org.eclipse.swt.widgets.Widget.error(Widget.java:565)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:481)
at org.eclipse.swt.custom.StyledText.getBlockSelection(StyledText.java:3163)
at org.eclipse.jface.text.TextViewer.computeSelection(TextViewer.java:2487)
at org.eclipse.jface.text.TextViewer.getSelection(TextViewer.java:2481)
at org.eclipse.ui.texteditor.AbstractTextEditor.doGetSelection(AbstractTextEditor.java:2890)
at org.eclipse.ui.texteditor.AbstractTextEditor$SelectionProvider.getSelection(AbstractTextEditor.java:1428)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:85)
at org.mozilla.javascript.gen.workspace___Andy_Refactorer_scripts_hello_js_6._c_script_0(Unknown Source)
at org.mozilla.javascript.gen.workspace___Andy_Refactorer_scripts_hello_js_6.call(Unknown Source)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3508)
at org.mozilla.javascript.gen.workspace___Andy_Refactorer_scripts_hello_js_6.call(Unknown Source)
at org.mozilla.javascript.gen.workspace___Andy_Refactorer_scripts_hello_js_6.exec(Unknown Source)
at org.mozilla.javascript.Context.evaluateReader(Context.java:1293)
at org.eclipse.ease.lang.javascript.rhino.RhinoScriptEngine.internalExecute(RhinoScriptEngine.java:223)
at org.eclipse.ease.lang.javascript.rhino.RhinoScriptEngine.execute(RhinoScriptEngine.java:202)
at org.eclipse.ease.AbstractScriptEngine.inject(AbstractScriptEngine.java:190)
at org.eclipse.ease.AbstractScriptEngine.run(AbstractScriptEngine.java:243)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
======

So, the problem seems to be, that my code does not run in the UI thread. I found some hint, that one can add

thread: UI

to the magic header. For Java there seems to the UIJob class, but how does this work in Javascript?

Thanks in advance for any help!

Ciao,
Andreas
Re: Javascript EASE script error : Invalid thread access [message #1860820 is a reply to message #1860818] Mon, 04 September 2023 19:07 Go to previous messageGo to next message
Christian Pontesegger is currently offline Christian PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior Member
Hi Andreas,

first a disclaimer: Running code in the UI thread is dangerous. If you do something wrong, eg "while(true);" your Eclipse IDE will not be responsive anymore and you have to kill your application.
That said you first need to enable UI scripting in the preferences: Scripting / Allow scripts to run code in UI thread. Normally all code executed runs in a separate thread. The UI module then offers a method executeUI() which allows to move parts of your code to the UI thread.
print("this is the standard thread")
executeUI('print("and this is the UI thread")')
executeUI('foo()') // calling a function from the UI thread
function foo() {}


the magic thread: UI keyword only works for scripts registered via preferences and started from eg the Script Explorer view. Then the whole code runs in the UI thread which is typically not a good idea. Try to keep UI sections as short as possible.

HTH
Christian
Re: Javascript EASE script error : Invalid thread access [message #1860822 is a reply to message #1860820] Mon, 04 September 2023 20:09 Go to previous messageGo to next message
Andreas Rückert is currently offline Andreas RückertFriend
Messages: 2
Registered: September 2023
Junior Member
Thanks a lot for your help! Still getting the thread access error, though, when I try to get the currently selected text. Maybe I need another solution for my problem. Thanks anyway!
Re: Javascript EASE script error : Invalid thread access [message #1860869 is a reply to message #1860822] Wed, 06 September 2023 20:46 Go to previous message
Christian Pontesegger is currently offline Christian PonteseggerFriend
Messages: 250
Registered: July 2009
Location: Graz, Austria
Senior Member
Try
executeUI("var content = widget.getText()")

print(content)


... or whatever you need to do in the UI thread

[Updated on: Wed, 06 September 2023 20:47]

Report message to a moderator

Previous Topic:Eclipse Editor does not recognize js file
Next Topic:Is it possible to import a Python script into another Python script in Eclipse Jython EASE?
Goto Forum:
  


Current Time: Mon Oct 07 20:53:57 GMT 2024

Powered by FUDForum. Page generated in 0.03560 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top