With regard to where to place the GTimer class you have four options
- Use the code that I gave in the main sketch window.
- Move the GTimer class code from above and put it in its own tab e.g. GTimer.pde
- Copy the GTimer.java file from the G4P library into the existing sketch folder
- Import the guicomponents library
Option 1 is out because you want (rightly) to keep the main sketch code short and tidy
Option 2 is fine and will work exactly the same way as option 1.
Option 3 will work if you
don't change the filename and you remove the statement
package guicomponents;
Option 4 makes the GTimer class avaialble along with the rest of the library.
The problem with option 4 is that you get the entire library included in your distribution but since it is only 87.1kB thats not a real problem. If you only need the GTimer class then option 3 is probably best in this case. (Note simply copying java source files from a library will not work in all cases).
The methods getData and sendData are executed by the timer at the appropriate time intervals whatever the frame rate (within reason) so you should not use frameRate in an attempt to match these processes.
a__g said
You never want to do something on the UI thread that can potentially halt, or stop working. That would lead to an unresponsive UI.
This is absolutely correct but the statement
The GTimer class hides the complexities of background threads as well, which makes it far superior for your purposes
is misleading because it would suggest that getData and sendData are run on a thread
seperate
from the UI thread and this is not true. It is only timer that runs on a different thread not the methods it triggers.
What actually happens is that at the appropriate time interval GTimer creates an event that is placed on the UI thread event queue. When the UI thread becomes active it then processes each event in the queue in turn, so if one event takes a long time to process then the UI can become unresponsive.
To avoid this then in your program should avoid waiting for the server if it is not ready so create a couple of boolean variable e.g.
boolean serverReadyToReceive, serverIsReadyToSend;
getData()
if(serverIsReadyToSend)
set serverIsReadyToSend to false
getData
endif
sendData()
if(serverReadyToReceive)
set serverReadyToReceive to false
sendData
endif
The boolean variables would be set back to true by some other part of your program. You want to avoid something like
.
getData()
while(serverIsReadyToSend is false)
wait
end while
getData
As this would freeze UI event handling, including mouse and keyboard until the server became ready.
HTH