C
C#3y ago
Thinker

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
becquerel
becquerel3y ago
depends how involved you wanna get i've used quartz for this and it worked well, and seen hangfire mentioned as a simpler alternative
Thinker
ThinkerOP3y ago
I would like at least an accuracy within a minute, so not very high standards Are Quartz and Hangfire libraries or something?
becquerel
becquerel3y ago
yeah quartz is the 'can do literally anything if you tinker with it enough' type
Thinker
ThinkerOP3y ago
Is it easy-ish to set up?
becquerel
becquerel3y ago
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
Thinker
ThinkerOP3y ago
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?
becquerel
becquerel3y ago
i don't think so - from what i remember that was one of its primary use-cases
Thinker
ThinkerOP3y ago
nice @Becquerel Do you happen to have any good tutorial/resource on using Quartz?
becquerel
becquerel3y ago
no, sorry it was a year ago i last used it
Thinker
ThinkerOP3y ago
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
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Ezlanding
Ezlanding3y ago
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
Thinker
ThinkerOP3y ago
literally no idea what that is
Ezlanding
Ezlanding3y ago
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...
Thinker
ThinkerOP3y ago
that image scares me
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Ezlanding
Ezlanding3y ago
So not just any background process then
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Thinker
ThinkerOP3y ago
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
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Thinker
ThinkerOP3y ago
I want to do stuff other than just schedule events in the app
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Thinker
ThinkerOP3y ago
Also the fact this mentions COM doesn't bode well
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Thinker
ThinkerOP3y ago
Fair I guess Again I can just wrap it in a class/service
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Auger
Auger3y ago
BlazeBin - nzsnlpyauajr
A tool for sharing your source code with the world!
Thinker
ThinkerOP3y ago
What's this "Cron" thing that keeping popping up in documentation?
Auger
Auger3y ago
It's a pattern that specifies a time-based schedule
Auger
Auger3y ago
Thinker
ThinkerOP3y ago
huh, neat
Auger
Auger3y ago
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 patterns
Thinker
ThinkerOP3y ago
To 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
Auger
Auger3y ago
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
Thinker
ThinkerOP3y ago
You mean NCrontab?
Auger
Auger3y ago
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
Thinker
ThinkerOP3y ago
Hmm, but what I wanna have like reminders and stuff at specific intervals before?
Auger
Auger3y ago
Are you executing just one job at a scheduled moment? Or are you trying to run a bunch of different jobs at different schedules?
Thinker
ThinkerOP3y ago
I guess different job at different schedules?
Auger
Auger3y ago
That might require a more robust solution then Or at least some clever adaptation to the library I showed
Thinker
ThinkerOP3y ago
This is also kind of why a timer doesn't seem very reliable for this
Auger
Auger3y ago
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
Thinker
ThinkerOP3y ago
Doesn't seem like there are any really good solutions
Auger
Auger3y ago
Can you explain a bit more specifically what sort of different schedules and jobs you were trying to implement?
Thinker
ThinkerOP3y ago
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.
Auger
Auger3y ago
Is this a Windows machine?
Auger
Auger3y ago
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 popup
Auger
Auger3y ago
Are you insisting on a C# solution?
Thinker
ThinkerOP3y ago
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
Auger
Auger3y ago
So a configurable shutdown service essentially
Thinker
ThinkerOP3y ago
yes And again I thought it'd be fun/useful to learn a bit about hosted services and whatnot
Auger
Auger3y ago
Did you want to hop on a VC? I've got some time, we could probably crank this out together
Thinker
ThinkerOP3y ago
eeehhh sorry but I really don't like talking in VC, plus I wanna do some other stuff rn But thanks for the offer
Auger
Auger3y ago
No worries I'd def start with a WorkerService project template
Auger
Auger3y ago
Auger
Auger3y ago
And modify the ExecuteAsync to use NCronTab and wait specified amounts of time from an IConfig
Thinker
ThinkerOP3y ago
Yeah, that was my thought
Auger
Auger3y ago
How's something like this?
Thinker
ThinkerOP3y ago
Works I guess
Auger
Auger3y ago
Auger
Auger3y ago
BlazeBin - kudhtfhfuabf
A tool for sharing your source code with the world!
Auger
Auger3y ago
At least, this is what I had in mind
devhl
devhl3y ago
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

Did you find this page helpful?