C
C#

help

Schedule code to execute at a specific time

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

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
What does MySQLConnector return in a SelectQuery if nothing is found?Question is in the titleCookies doesn't work after PC restartAfter I login to the website the cookies remembers me even after browser shutdown but if I restart mhow do i print an arraylist [Answered]im new to c and ive been trying to print an arraylist and im still not too sure now toFilter and give back a new object with LinqSimulating the current situation: ```cs class MyDTO{ int Id {get;set;} int IdOfSomethingElse{get;seQuestPDF round borderIs there any option of rounding border corners in questpdf?Inject http client only for specified service [Answered]I have a RemoteApiClient that I want to inject to the FooBarService and only to the FooBarService. Tsolved [Answered]its giving result from previous loopHttp 400 Bad Request Response [Answered]POST controller in WebAPI. ``` [Route("api/[controller]")] [ApiController] public class EventsCMany-To-Many with same fields of same typeI have a tables Users and Transactions. I want to make my Transactions store two entities: sender anObservable collectionHello, i'd like to know/understand why the event Collectionchanged isn't triggered when i change a vHow do I split these strings?If I have these inputs, wich could be random numbers. How do I split the string into two parts wher?.Show() Is not working [Closed]I'm creating my "antivirus" but when I put it to display the new form with .Show it doesn't work theHow to stop two right-aligned elements wrapping aroundI have a navbar component with these two elements, the signin/signout links and the search form: ```Does anyone have any thorough resources or documentation on how to implement a custom Identity setupI am trying to implement Identity on a project that does not already include it. I have found some rDrop composite PK and add new column PK with identity with EF Code firstHello. I have following table ClaimFeeType(picture below). I want to remove composite PK constraint How can I start the task by string?I need something like this, is this possible to do? void Main() { var input = Console.ReadLine(How do I deal with a browsers own autocomplete suggestions?I have the following input in an Angular component: ```html <input autocomplete="family-name" formCoGithub action fail with appservice at Run azurewebapps-deployv2This is where it break here is the full job ```yml # Docs for the Azure Web Apps Deploy action: httstring doesnt work?i wanna make a variable for all methods.. so i dont have to write the variable over and over again..Generate PDF file in WPF appHi, I have a little problem. I am writing app in WPF and I want to create PDF with some graphs etc.