Universal Robots Forum

Change datamodel in thread

Hi,
I have a URCap with a button starting a Java thread, which moves the robot around its workspace and doing some measurements from the secondary client interface . After the movements and measurements are done, I want to change the datamodel to contain a variable telling whether the operation was successful or not.
However, it is not possible to change the datamodel in other threads than the UI thread (as far as I understand?), which seems to leave me with two options:
I can make the UI thread wait until the movements/measurements are done and then change the datamodel, or I can put a button in my URCap that should be pushed afterwards, which saves the variable to the datamodel. Neither of these options are particular elegant, the first option would freeze the whole controller and the second adds extra an extra unintuitive action to perform.

Is there any way around this problem? E.g. other parts of the code that are run in the UI thread?

Best Regards,
Thomas

1 Like

So I figured this out if anybody is interested. The solution i simply to add an event to the EventQueue, that performs the desired changes to the datamodel, like this:

	try {
		EventQueue.invokeAndWait(new Runnable() {
			@Override
			public void run() {
				undoRedoManager.recordChanges(new UndoableChanges() {
					
					@Override
					public void executeChanges() {
                                                 // Execute changes here!!!
					}
				});
			}
		});
	} catch (InvocationTargetException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (InterruptedException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
1 Like