[SailfishDevel] Scheduling code for execution at x seconds since epoch
Arvid Fahlström Myrman
behold at behold.se
Sun Mar 16 17:41:15 UTC 2014
On Sunday 16 March 2014 16:17:04 Ove Kåven wrote:
> Den 16. mars 2014 14:14, skrev Arvid Fahlström Myrman:
> > Yes, I've been playing around with BackgroundJob from nemo-keepalive a
> > little since I wrote that. It seems to mostly work similarly to
> > Insomniac, with the added benefit of preventing suspend after triggering
> > until BackgroundJob's finished() method is called. It's a bit finicky to
> > use though, since it seems to always trigger once immediately upon being
> > enabled, and it doesn't have a repeat property that can be set to false,
> > so to prevent it from running more than once you have to set enabled to
> > true when it's triggered and hope that it doesn't have time to schedule
> > the next event
>
> "Set enabled to true"? Do you mean setting it to false? Or how exactly
> are you using it? As far as I can tell, you should set enabled to true
> to start the timer, and enabled to false to stop it.
Yes, you're right, I meant to write "false".
> You should probably set enabled to false *after* your job is done,
> because setting it to false will also do what finished() does.
>
> If the timer were to elapse again before you're done, this would be
> ignored as long as you haven't called finished or set enabled to false.
> So you shouldn't be getting more than one trigger of this kind (in theory).
After testing some more, it seems that my problem was that I was setting
enabled to false after running finished(). Take this code for example, which is
a simplified version of my original test:
Item {
Component.onCompleted: backgroundJob.enabled = true
property real startTime: Date.now()
Timer {
id: timer
repeat: false
interval: 1000
property int i: 0
onTriggered: {
i += 1
console.log("Timer triggered at", Date.now() - startTime,
"milliseconds since application start")
console.log("Timer has been triggered", i, "time(s)")
backgroundJob.finished()
if (i == 2) {
backgroundJob.enabled = false
console.log("BackgroundJob disabled at", Date.now() - startTime,
"milliseconds since application start")
}
}
}
BackgroundJob {
id: backgroundJob
frequency: BackgroundJob.Range
minimumWait: 10
maximumWait: 15
onTriggered: {
console.log("BackgroundJob triggered at", Date.now() - startTime,
"milliseconds since application start")
timer.start()
}
}
}
Intuitively, I would expect backgroundJob to cease all activity when enabled
is set to false, meaning that I would only expect the Timer to trigger two
times in total. However, in reality the output that I get is the following:
BackgroundJob triggered at 71 milliseconds since application start
Timer triggered at 1121 milliseconds since application start
Timer has been triggered 1 time(s)
BackgroundJob triggered at 21374 milliseconds since application start
Timer triggered at 22379 milliseconds since application start
Timer has been triggered 2 time(s)
BackgroundJob disabled at 22389 milliseconds since application start
BackgroundJob triggered at 38367 milliseconds since application start
Timer triggered at 39371 milliseconds since application start
Timer has been triggered 3 time(s)
If I move backgroundJob.finished() to after the if statement the timer only
triggers two times in total, however. I'm not sure if this should be
considered a bug or what (maybe it makes some amount of sense somewhere down
the line), but I would definitely not expect another triggered signal after
having set enabled to false.
> To start the timer using a range (instead of just a frequency), set the
> minimumWait and maximumWait properties as desired, and the frequency
> property to "Range". Then start the timer by setting enabled to true.
> (The timer should not be enabled before you've set all these properties,
> or you might risk funny effects.)
>
> > Regarding timed, I have looked at nemo-qml-plugin-alarms previously, but
> > from what I could tell (though I'll admit I didn't really take the time
> > to actually understand how it works, so it's very possible that I wasn't
> > using it correctly) it is not really possible to schedule a specified
> > piece of code for execution using it;
>
> With the C++ interface it's possible (well, at least it's possible to
> start any program or send any D-Bus message). Perhaps the QML interface
> is less flexible in that regard...
>
> _______________________________________________
> SailfishOS.org Devel mailing list
More information about the Devel
mailing list