The easiest way to schedule a function to run at a specific time using C #

If I had many messages in the database that I wanted to send, and each line indicated the date and time the message was sent and the flag if it was sent.

They will not always be at fixed intervals, and more than one message can be sent at the same time.

In this case, he will simply put them in the queue and put them in order when they are created.

The easiest way is simply to make a function that starts again and again, as soon as it finishes, it starts again

So this is:

  • Start Run and check current date / time
  • Check for any unsent messages
  • Send all messages due to exit before and before launch
  • Start all over and take the current date / time.

My problem is whether the method will just be terribly inefficient to work continuously, possibly for hours or days without actually sending the message.

The main voltage in this case, I think, will be placed in the database, it will constantly come across with a request.

Is there a better way to plan for something like this.

Or just do it, but every time it starts, wait 5 minutes before starting it again.

Is there anything suitable for planning in Workflow 4?

+4
source share
13 answers

You can always pre-read the next time value in a series and make one dream until then, instead of looping through short dreams over and over again.

Not sure if this is as thorough as you want,

+2
source

Maybe there is a compiled view in the database that returns messages that are not sent (I assume there is a flag in each record?) And for which the estimated time to send is up to the current time. Then the Windows application or the console application at the scheduled interval can fall into this view (which can be configured quite well in the database, I think) and send any messages returned to them.

+1
source

You can use the Windows service to accomplish this. Or, if you use MSSQL, you can even use the SQL Server Agent job.

+1
source

Several answers suggested sending some messages and then causing a dream until the next message was sent.

How do you sleep in this case, everything is important.

You can - in theory - tell a thread to sleep for several hours, however, if during this time the application (or service) should be turned off, then you have problems. The process will be completed, no cleaning will be performed. This is less than ideal.

Do not confuse the concept of a survey for work and sleep between surveys.

If you need to wait 5 minutes (or 5 hours) until the next database poll, that’s fine, but you’ll never want to * sleep for more than a second or two at a time.

What will i do.,.

Write a Windows service. The service has one active thread that polls the database, see Any messages that should send and sends them. He will then poll the custom delay (1 minute, 5 minutes, 1 hour, what ever fits). However, he will never sleep for more than a second while he waits for a database survey.

If you can be sure that messages can be added only for sending after the last message in the database? If so, you can check the time of the next message and not poll until this time.

However, if I find that the next message does not need to be sent within 5 hours, is it possible that while I wait for the message to be sent in 30 minutes?
If so, you will never be able to trust “Next Message Time” and do not poll until you need to constantly poll your fixed NB interval to say again, your poll interval and your wait interval are not the same thing.

+1
source

How about writing a Windows service that does this for you. This Windows service will run in the background and check the current time with your records in a host interval (for example: every 5 minutes) and send emails to people and update the corresponding records in your tables to set the flag of the sent email message to true

You might even have an SQL job that selects records that are not being sent, and matches the current time, and calls a stored procedure that calls the point network assembly to send email. A point network host can use SMTPClient to send messages.

0
source

It depends on what you use. Using a scheduled task or service is acceptable for the scenario you are describing.

You must be careful, although you do not link resources if the process runs too often. This may be more effective so that it is performed less frequently during peak periods and more often during off-peak times.

0
source

Whatever method you choose (make a Windows service, use a task scheduler, etc.), remember that your initial proposal is exactly what is called busy waiting , which you should avoid if you really don't know what are doing.

0
source

What you are describing is not so bad if you expand it "when there are no messages that you have to select, the next time the message appears, and sleep until then."

Alternatively, use a database with "notification support", which makes the whole thing event-driven, i.e. The database sends you an event whenever a message occurs.

0
source

I recently implemented a Windows service that used the IntervalHeap class in the C5 collection's class library. Then I added a save layer that saves the element tracks and their intervals in case of service stop / failure.

It works for several months and works very well.

0
source

you can use this .NET Scheduled Timer to check the time and function maintenance (sending messages) at specific time intervals ....

0
source

I would say create a windows service using a timer. he can sleep for a configured number of seconds and then compare the date and time from the database. if it matches, then send an email and set a flag in the database for sent emails.

0
source

We do this at a financial institution to send internal emails from our intranet applications. Every 15 minutes, the scheduling program (the corporate scheduler, not the Windows scheduled task) starts the task. We have a view called PendingEmail on top of a table named EmailQueue , which lists only what you need to send this to get around (the EmailQueue table has a PopDate , which is the effective date when email should be sent). The app launches email messages for everyone found in the PendingEmails .

The task sends out the maximum size of email messages every 15 minutes, marking each entry, whether it was sent successfully or whether there was an error (invalid email address, etc.) and what was the Exception , and whether we would like to try again send it next time. It updates the EmailQueue table immediately, and not each record separately. The batch size was set in place so that the task did not take more than 15 minutes and stamped on its own.

I don’t know that every poll consumes all these resources so often if you are not going to do it every 5 seconds or something like that. If you send millions of messages, you may need to distribute the work across multiple computers. If you are going to write some custom code, I would use Timer over Thread.Sleep() and set Timer to a mark every 5 minutes or any interval that you would like to execute. An event is triggered on every tick that would subscribe to start a procedure that sends your messages.

See this post in Thread.Sleep() compared to the Timer class:

0
source

Many databases allow you to trigger events using triggers, for example. 'after insertion'. A trigger is triggered by a database process / thread, and the actions that it can take are dependent on the database. For example, it can call a C or java procedure that signals a semaphore with the name your mail client is waiting on, or exec. email application. Look at the “trigger” or “create trigger” for your database.

0
source

Source: https://habr.com/ru/post/1381910/


All Articles