Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repetitive tasks in specific time intervals?

For example, I want to call a function in specific time intervals (hourly or daily tasks.)

I m already using Timer. But it is working as long as my local server is working. For Example:

public static void StartYahooWeatherManager()
{
    Timer t = new Timer(1000 * 60 * 60 * 6  /* 6 hours */);
    t.Elapsed += new ElapsedEventHandler(WriteYahoWeatherRssItemToDb);
    t.Start();
}

private static void WriteYahoWeatherRssItemToDb(object o, ElapsedEventArgs e)
{
    YahooWeatherManager.InsertYahooWeatherRssItem(YahooWeatherManager.GetYahooWeatherRssItem());
}

I write weather info to db, in 6 hour intervals. Project is already in Host. Timer event is working as long as my computer local server is running.

Question:

What is the wrong about my code? (Or logical wrong to using timer in WebProject?) Why timer is only working while local server working?

What is the best way to manage time interval tasks? (not need asp.net)

I hope, I can explain the problem. Thanks.

like image 741
AliRıza Adıyahşi Avatar asked Nov 30 '12 15:11

AliRıza Adıyahşi


People also ask

Why do I have so many repetitive tasks?

The duplication of tasks often happens due to a lack of clarity on who in your team is supposed to work on what. Repetitive tasks are often regarded as monotonous, low-level tasks that could be better organized in order to leave more time for tasks of higher value. How much time do you spend on recurring tasks?

What are recurring tasks?

Recurring tasks (also known as repetitive or routine tasks) are activities that have a tendency to repeat on a certain basis, either in their entirety or to a varying degree. Some recurring tasks occur at regular time intervals.

What is the difference between intervalminutes and scheduledtime in Windows service?

IntervalMinutes: It is used when Mode is set to Interval. It consist of the Interval value in Minutes after which the Windows Service will perform a task. In other words it is the delay value. ScheduledTime: This setting is used when the Mode is set to Daily. It is used to notify the Windows Service the time it should perform a task.

How can I perform repetitive tasks in a positive way?

You can follow the tips below to perform repetitive tasks in a positive and successful way: Consider switching tasks with another employee. Try changing the method with which you complete your tasks. Strive for improvement by recording what you did to finish a task quickly or effectively. Exercise self-care by taking short breaks.


1 Answers

The reason why the timer stops is because this timer runs in your ASP.NET application which itself is hosted on a web server. When this server stops or recycles the application, the AppDomain is brought down along with all threads you might have started. That's why it is a very bad idea to write repeating background tasks in an ASP.NET application. Phil Haack blogged about the dangers.

The correct way to do this is to host this code in a separate process outside of your ASP.NET application. This could be a windows service or a console application which is scheduled to run at regular intervals using for example the Windows scheduler. This way the code is guaranteed to always run.

like image 133
Darin Dimitrov Avatar answered Sep 21 '22 05:09

Darin Dimitrov