1

I have been reading loads about timers, and options for achieving my goal, and I believe I need to use system.timers.timer and set the interval correctly.

I think I know how to do this but what I am unsure of is where to do it, do I do so in my view, my controller, global.asax?

Maybe I shouldn't use a timer at all?

What I'm looking for is the best way to run an if statement on the hour, every hour, and update the view depending on the results of that if

Dave New
  • 38,496
  • 59
  • 215
  • 394
Karl Humphries
  • 328
  • 1
  • 4
  • 15
  • http://stackoverflow.com/questions/1206507/how-to-refresh-the-page-in-asp-net-let-it-reload-itself-by-code – Habib Nov 13 '12 at 09:12
  • You don't mention if you are running on any cloud platforms, but Windows Azure can handle this using it's various roles schemes. – Arran Nov 13 '12 at 09:30

4 Answers4

4

Usually a web app (MVC or WebForms) isn't the best place to run scheduled tasks. It can be done, but you will get things like IIS recycling application pools and other anomalies, which although useful to a web app, could get in the way of reliably scheduling tasks.

A lot of developers (and my favourite too) schedule their tasks in a Windows Service. This can be installed and can be set to gracefully start and stop when the server starts and shuts down. You can then set up logging and other health monitoring to monitor the state of your scheduler service.

However if your tasks is purely SQL based you may wish to use SQL Server's inbuilt scheduling (or similar for any other database). Another alternative is to use the operating system's scheduler.

Edit

In regard to updating your view with the results, you can store and update the status of your scheduled tasks in the database with columns like 'TaskStatus' (New, Waiting, Running, Aborted, Failed, Cancelled, Completed) and 'TaskResult' (probably empty for success or the error message from a failure). You can then show and filter this information on your results view by retrieving it from the database.

Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
2

JavaScript - On Your Page: You can redirect to the controller > action that is your required function through JavaScript:

<script type="text/JavaScript">
setTimeout("location.href = '/YourDefineUrlPathHere';",1500);
</script>

HTML Meta tag - On Your Page: Other is you can do it through meta tag if you want to call your view action again (in other words refresh it)

<head>
<meta http-equiv="Refresh" content="15;url='/YourDefineUrlPathHere'"> 
<!—‘15’ is number of seconds you want to wait-- >
</head>

Timers – in your controller

aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(YourMethodHere);
aTimer.Interval = 2000;
aTimer.Enabled = true;

See MSDN: Timer

  • Timers are not very efficient solution if you want to keep caaling for a long amount of time

However another way and I guess more ‘standard’ way to achieve your result could be through Window Services if you want to pursue it in this direction this link might help you a lot. Introduction to Windows Service Applications

SajjadHashmi
  • 3,795
  • 2
  • 19
  • 22
  • I ended up going for a simple bit of code to work out how long until the turn of the hour and then setting the result as the interval on a refresh. Thanks for your comments – Karl Humphries Nov 13 '12 at 16:01
0

What do you mean by "update the view"?

An MVC View is a class - you can only "update" objects as they have values. Views are instantiated when a user requests the page.

If you mean you want the user to have a page open in their browser all the time and it gets refreshed every hour, then you could use a timer in javascript on the client to reload the page every hour.

If you mean you want to update the results of any user opening your page depending on data that changes every hour, you can store the last update time in your database and then when the view runs check if the data is more than an hour old. If it is, run your if statement in your controller.

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • Hi, I do indeed mean that I want to refresh the page on the hour, but i wanted to see if it was possible to re run through the if statement i have and sort of "push" the result to the browser so as not to refresh the whole page. I think I see now that a simple refresh is going to be the best solution. Thanks for your comments – Karl Humphries Nov 13 '12 at 15:14
-1

Use Azure Functions. You can run scheduled tasks using a timer trigger. It's pretty good.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-overview

nmit026
  • 3,024
  • 2
  • 27
  • 53