tags:

views:

753

answers:

4

I'm using c# to communicate with twitter and i need to code a schedule system to send twitter messages at a user defined date.

The messages to be sent are in a database with the date and time of deliver.

Which is the best method to check the db for scheduled messages and send it when the time arrives?

+1  A: 

How accurate do you need the timing to be? Could you get away with polling the database every 5 minutes, saying "Tell me all the messages which need to be delivered before current time + 5 minutes" and then sending them all? (And marking them as sent in the database, of course.) Does it matter if they're a bit early or late?

You can do the scheduling on the C# side to make it more accurate, but unless you really need to I'd stick with a very simple solution.

(Depending on your database there may be clever ways of getting callbacks triggered etc... but again, I'd stick with the simplest solution which works.)

Jon Skeet
I'll be good with a 5/10 minutes delay. What solution do you propose?
jvalente
As the answer says, poll the database every 5 minutes (e.g. using a Timer in a Windows service). Fetch everything to be sent, send it, mark it as sent. Job done.
Jon Skeet
Well, i've created a Windows Service but i need it to be a Network Service(i must access a remote database). However i can't start the service due to a "Error 5: Access is denied". Probably i must start it as administrator. Does this any sense? My application needing administrator privileges?
jvalente
A: 

There are several ways to do this, but I guess the best way is to set up a Windows Service that will periodically poll (frequency is up to you) the DB for any scheduled tweets that hasn't been sent.

Needless to say you'll need to handle scenarios such as the Internet connection or DB being down, etc.

Jon Limjap
+1  A: 

In addition to the windows service option (or background thread), you could just set up a scheduled task to run an app that polls the DB and sends the tweets once every defined interval.

Windows schedules can be setup using C# if needed and are really easy to set up manually.

Sam Saffron
I've found that that task scheduler is most recommended to user apps. So that solution don't fit in this case.
jvalente
A: 

In fact the solution consists in using a windows service but it can't communicate directly with the ASP.NET MVC app. I've added a Web Service that handles the task and a System.Threading.Timer in Windows Service to periodically call the Web Service.

jvalente