I need to perform data analysis on files in a directory as they come in.
I'd like to know, if it is better,
to implement an event listener on the directory, and start the analysis process when activated. Then having the program go into sleep forever:
while(true), sleep(1e10), end
or to have a loop polling for changes and reacting.
I personally prefer the listeners way, as one is able to start the analysis twice on two new files coming in NEARLY the same time but resulting in two events. While the other solution might just handle the first one and after that finds the second new data.
Additional idea for option 1: Hiding the matlab GUI by calling frames=java.awt.Frame.getFrames
and setting frames(index).setVisible(0)
on the index
matching the com.mathworks.mde.desk.MLMainFrame
-frame. (This idea is taken from Yair Altman)
Are there other ways to realize such things?
In this case, (if you are using Windows), the best way is to use the power of
.NET
.There are different event types, you can use the same callback for them, or different ones:
Where
eventhandlerChanged
is your callback function.There is no need to use
sleep
or polling. If your program is UI based, then there is nothing else to do, when the user closes the figure, the program has ended. The event callbacks are executed exactly like button clicks. If your program is script-like, you can use an infinite loop.More info in here: http://www.mathworks.com/help/matlab/matlab_external/working-with-net-events-in-matlab.html