Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to schedule a function to run at a specific time using C#

If I had a lot of messages in a database that I wanted to send, and each row specified a date and time to send the message, and a flag for if it has been sent.

These won't always be at fixed intervals, and more than 1 message may want to be sent at the same time.

In this case it would just queue them up and send in order of when they were created.

Is the easiest thing to do just to have a function that runs over and over again, once it completes it just runs again

So it would:

  • Start Running and check the current date/time
  • Check for any unsent messages
  • Send all the messages due to go out before and up to the time it started running
  • Start all over again and take the current date/time

My problem with this is, would it just be horribly inefficient to continuously have a method running, possibly for hours or days without actually sending a message.

The main strain in this case I think would be put on the database, it would constantly be getting hit with a query.

Is there a better way to schedule something like this to happen.

Or just do the above but every time it runs make it wait for 5 minutes before running again.

Does Workflow 4 offer anything suitable for scheduling perhaps?

like image 854
Dan Harris Avatar asked Nov 18 '11 14:11

Dan Harris


1 Answers

You could always do a pre-emptive read of the next time value in the series and do a single sleep until then, instead of looping through short sleeps over and over.

Not sure if that's as elaborate as you want though

like image 168
Jean-Bernard Pellerin Avatar answered Oct 11 '22 21:10

Jean-Bernard Pellerin