Two quick questions about threads.
One is a programming question, one is “best practice”.
For the best practice, I want to end a program after a given period of time, using a timer. Works perfectly as I want it to. No issue there. Should I use a thread to do this? Is there an advantage or disadvantage?
For the programming part, I take the code I have in my timer, and after creating a thread, place that code in the thread, as shown below:
// when the timer runs out, which means the screensaver stops, blank the display.
// for this we need a shell script.
var blankTheDisplay as New Shell
//take the number entered in the screensaverMovieListWindow Textfield1 box *60 (seconds in a minute) * 1000 (to make milliseconds)
// every time it fires add a second (1000 ms)
app.endTheProgram = (10 * 60 * 1000) / 1000
if app.incrementTheSeconds >= app.endTheProgram then
blankTheDisplay.Execute ("pmset displaysleepnow”)
Quit
else
app.incrementTheSeconds = (app.incrementTheSeconds +1)
System.DebugLog " The end time in ms is : " + app.endTheProgram.ToString + "The incremented seconds are : " + app.incrementTheSeconds.ToString
end if
My system.debug shows it only seems to fire 1 time:
10:50:05 PM : sharableMovie Launched
10:50:33 PM : The end time in ms is : 600 The incremented seconds are : 1
10:51:03 PM : sharableMovie Ended
Use a Timer unless there is a compelling reason to do it another way.
The purpose of a Timer is to trigger an event after a period of time has elapsed. The purpose of a thread is to divide a process into discrete tasks. Timers have the advantage of being less complicated to implement and debug compared to threads.
If a Timer is only firing once you need to set to the .RunMode property to RunModes.Multiple
If a thread is only firing once, well that is how threads work. Call the .Start method to trigger the Run event and when .Run exits the thread stops because execution is complete. The thread will not run again until .Start is called again.
Also.
Nothing to do with your question but with simplification in mind…
//take the number entered in the screensaverMovieListWindow Textfield1 box *60 (seconds in a minute) * 1000 (to make milliseconds)
'multiplying by 1000 then dividing by 1000 is redundant.
'app.endTheProgram = (10 * 60 * 1000) / 1000
app.endTheProgram = (10 * 60)
You’d use a thread if you have a long running calculation to perform and want the UI to remain responsive.
I use a thread to perform housekeeping tasks once a week, in my app. I could use a timer, but then all the code for the housekeeping would be in the timer’s event handler, which, as it runs on the main thread, would risk blocking the UI (OK, perhaps not at 02.00 when it runs, but some night bird might get annoyed).
I use a thread for processing socket data, the arrival of which is not under my control. The thread’s Run event will request data, and if there is none then it pauses the thread. The thread is resumed by the DataAvailable event when data arrives; that event does nothing else - the thread reads the data. Again, this ensures that the UI is not bothered.