2

I am trying to achieve the following: I am using MySQL and ASP.NET. I am creating a counter in a table. I want that counter columns to be reset to zero every day. Is there any option to achieve this and how.

I don't want to use admin tasks in Windows, and not to create .exe file. I prefer be able to do that in C#, in code.

The way I thought in solving this is to create an Application variable that will be set to false. In the application page, I will query the DateTime.Now and get the current hour. If the hour is 12:00 and the Application variable is false, run a function, else, do nothing. If the time is 13:00 and Application variable equals to false, set it to true.

I do that in an hour difference just to make sure that it runs. Every user that visited my page will trigger that if statements. However, the function will run only once a day.

Is it a good solution?

Liron Harel
  • 10,819
  • 26
  • 118
  • 217
  • 6
    What do you do if nobody visits your application during that hour? ASP.net is not designed for this sort of thing, this is what scheduled tasks/services are for. – Paddy Jul 30 '12 at 11:55
  • Sם I will make it 11 hours difference checks – Liron Harel Jul 30 '12 at 11:58
  • Inorder to create a scheduler, there is no need of exe or even windows services. You can even schedule your .aspx page itself. Why cant you go for a scheduler? – shajivk Jul 30 '12 at 12:01
  • 1
    As paddy said, asp.net is not designed for this... The problem is that you can never be sure your app doesn't get recycled many times between 12 and 13 either, ie. you have absolutley no control over the lifecycle – Daniel Jul 30 '12 at 12:02
  • Any reliable online cron services. There are many, most of them looks amateur – Liron Harel Jul 30 '12 at 12:34
  • Does Task Scheduler support calling a URL, loading a web page? – Liron Harel Jul 30 '12 at 12:45
  • I believe that there are also scheduled jobs/events in MySql (although I am not much of a MySql user...). – Paddy Jul 30 '12 at 12:56

2 Answers2

0

After reading your comments, I agree that I should put more effort towards a more efficient solution.

I've decided to use Microsoft Task Scheduler with a VBS script as follows:

1) Create a vbs script that loads a specific page and even save the result to a specific file, like this:

sSrcUrl = "http://www.this-page-intentionally-left-blank.org/"
sDestFolder = "C:\"
sImageFile = "filename.txt"
set oHTTP = WScript.CreateObject("MSXML2.ServerXMLHTTP")
oHTTP.open "GET", sSrcUrl, False
oHTTP.send ""
set oStream = createobject("adodb.stream")
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
oStream.type = adTypeBinary
oStream.open
oStream.write oHTTP.responseBody
oStream.savetofile sDestFolder & sImageFile, adSaveCreateOverWrite
set oStream = nothing
set oHTTP = nothing
WScript.Echo "Done..." 

2) Create a Task to run this script file (cron.vbs) every day in a specific hour.

I should run this on a Windows Server 2008. I've tested it locally on Windows 7 and it works great.

I don't see any disadvantages, unless I am missing something.

source: Recommended method for loading a URL via a scheduled task on Windows

Community
  • 1
  • 1
Liron Harel
  • 10,819
  • 26
  • 118
  • 217
  • I think you missed the points of the comments, it is not to use task scheduler to open the page so it can run the command, use task scheduler to run the query you want itself. – Scott Chamberlain Jul 30 '12 at 14:03
0

If you want the counters to be reset by 12:00, but noone visits your site until 13:15, you don't really care that that reset happens at that later time: there was nothing to count in the meantime.

What you can do is not remember a boolean ('did a reset'), but a date ('reset done on'). If the current time is after 12:00, check the stored data against the current date. If they don't match, reset the counters and update the stored date.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111