I have a question regarding adding a function to be ran in the Processing Animation Thread or, in other words, to be ran after
draw() in a similar fashion as the
Basically I am developing an application that receives UDP messages from Max and, when a UDP message is received (I’m using the function receive() from the UDP library to handle this) it triggers a function that affects the draw() function. The problem is that the UDP library seems to be running in a different thread, which is cuncurrent with the Processing Animation Thread and causes a race condition, that leads to acessing an out of bonds value in one of the tables used in draw(). If instead of receiving an UDP message to trigger the function, I use the keyPressed(), everything runs smoothly.
In the Processing source code inside core/PApplet.java, I’ve found this comment:
Processing uses active mode rendering. All animation tasks happen on the
“Processing Animation Thread”. The setup() and draw() methods are handled
by that thread, and events (like mouse movement and key presses, which are
fired by the event dispatch thread or EDT) are queued to be safely handled
at the end of draw().
For code that needs to run on the EDT, use EventQueue.invokeLater(). When
doing so, be careful to synchronize between that code and the Processing
animation thread. That is, you can’t call Processing methods from the EDT
or at any random time from another thread. Use of a callback function or
the registerXxx() methods in PApplet can help ensure that your code doesn’t
do something naughty.
Basically I would like to queue an event to be safely handled at the end of
draw(), inside the UDP thread. I tried the
invokeLater() function inside the
receive() but it still had the same out of bonds error.
Another thing I think I can do is, inside the UDP
receive(), generate a keyPressed event, any tips on how to do it?
Not sure I’m very clear but basicaly, I want to have a fucntion that handles UDP messages the same way that keyPressed() handles key presses.
Thank you very much!