Schedule code to execute at a specific time
Essentially I want to have a
DateTimeOffset
and execute code specifically at that date/time. What would be the best way to do this? I feel like timers would be a rather fragile approach.63 Replies
depends how involved you wanna get
i've used quartz for this and it worked well, and seen hangfire mentioned as a simpler alternative
I would like at least an accuracy within a minute, so not very high standards
Are Quartz and Hangfire libraries or something?
yeah
quartz is the 'can do literally anything if you tinker with it enough' type
Is it easy-ish to set up?
it uses its own terminology for stuff but not tooooo bad
you create a class to define your job and another for any data you want to pass along, were the main things
Doesn't sound too bad
I'm planning to use this for a background service, is there anything which I should be aware of in relation to that?
i don't think so - from what i remember that was one of its primary use-cases
nice
@Becquerel Do you happen to have any good tutorial/resource on using Quartz?
no, sorry
it was a year ago i last used it
Quartz seems almost waaay to overkill for this. I literally just want to schedule something for a single time. The Quartz documentation makes it seem unnecessarily difficult to do this.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Random question:
Is a “daemon” a correct way to refer to a background service like that? I’ve seen the term being used without much explanation before
literally no idea what that is
Daemon (computing)
In multitasking computer operating systems, a daemon ( or ) is a computer program that runs as a background process, rather than being under the direct control of an interactive user. Traditionally, the process names of a daemon end with the letter d, for clarification that the process is in fact a daemon, and for differentiation between a daemo...
that image scares me
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
So not just any background process then
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I mean like annoying to use. Ideal would being able to just schedule an action for a specific time. Although I suppose I could just create a wrapper class/service around a timer.
Wasn't there supposed to be a new fancy timer in .NET 6?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I want to do stuff other than just schedule events in the app
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Also the fact this mentions COM doesn't bode well
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Fair I guess
Again I can just wrap it in a class/service
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I use NCronTab
https://paste.mod.gg/nzsnlpyauajr/0
BlazeBin - nzsnlpyauajr
A tool for sharing your source code with the world!
What's this "Cron" thing that keeping popping up in documentation?
It's a pattern that specifies a time-based schedule
Crontab.guru - The cron schedule expression editor
An easy to use editor for crontab schedules.
huh, neat
Very
Alternatively, if you're executing in the context of a Linux machine, setting up a CRON tab that just executes your program at a regular interval might be easier
crontab -e
This similar to Windows task scheduler, except Windows doesn't use Cron patternsTo be fair, the thing I wanna make isn't much more complex than a background service which shuts down the system at specific times and also does some Windows toast interaction, but still I thought this would be a decent opportunity to learn about hosted services and stuff
So if this is a hosted service that you want running constantly, I would recommend my first post, but if it's something that can just be a spawned process every now and then and doesn't have to be constantly running, perhaps something like Cron tab or task scheduler is relevant
You mean NCrontab?
Yeah, just a simple library that yields a time until the next scheduled moment to do something.
For that project type I'd probably use a WorkerService
Hmm, but what I wanna have like reminders and stuff at specific intervals before?
Are you executing just one job at a scheduled moment? Or are you trying to run a bunch of different jobs at different schedules?
I guess different job at different schedules?
That might require a more robust solution then
Or at least some clever adaptation to the library I showed
This is also kind of why a timer doesn't seem very reliable for this
The advantage to the library I showed, is it's essentially just a parser for a standardized pattern
It gives you a time span, and it's up to you how to schedule everything around that. You could use it to parse a bunch of different schedules and write custom logic to fire off your jobs on those different schedules
Or you could potentially adopt a more robust scheduling solution with something like hang fire, but I don't know if that's overkill for you. A simpler library for my own use case
Doesn't seem like there are any really good solutions
Can you explain a bit more specifically what sort of different schedules and jobs you were trying to implement?
Essentially this is all just for a background timer to shut off the system at a specific time (because I'm bad at keeping times lmao), so I want to have one job which is schedules for a time and which shuts down the system, and a couple jobs which are schedules for like 5, 15, 30, and 60 minutes before that time which creates some Windows toasts as reminders.
I mean, you could potentially do this all with Task Scheduler then
Just run a these at the right times:
60m, 30m, 15m,
msg * "60 minutes until your scheduled shutdown"
5m shutdown -s -t 300
shows that popupAre you insisting on a C# solution?
I mean not really, but just having times stored in some config file somewhere and have a background service do it automatically would be pretty nice
So a configurable shutdown service essentially
yes
And again I thought it'd be fun/useful to learn a bit about hosted services and whatnot
Did you want to hop on a VC? I've got some time, we could probably crank this out together
eeehhh sorry but I really don't like talking in VC, plus I wanna do some other stuff rn
But thanks for the offer
No worries
I'd def start with a WorkerService project template
And modify the ExecuteAsync to use NCronTab and wait specified amounts of time from an IConfig
Yeah, that was my thought
How's something like this?
Works I guess
BlazeBin - kudhtfhfuabf
A tool for sharing your source code with the world!
At least, this is what I had in mind
You could try my library. You can inherit ScheduledService (which itself inherits BackgroundService), then override the GetDelayBeforeExecutionAsync method to set the time span. If the time span is fixed, then you set it in appconfigs
https://github.com/devhl-labs/ScheduledServices