MayureshP
MayureshP

Reputation: 2634

Include scheduling in web-application in asp.net

I wanted to run scheduling process in asp.net periodically in web application.

In brief,My database table is having date & deadline Hrs.I want to calculate expected dateTime from both then another table get updated (inserts 1000s of records) periodically & also want to run process of mail sending according to that calculation for the same.

This is expected scheduled process which should be executed periodically.

Upvotes: 0

Views: 570

Answers (4)

Michiel Overeem
Michiel Overeem

Reputation: 3992

Check out this old article from Jeff Atwood: Easy Background Tasks in ASP.NET

Basically he suggests that you use the cache expiration mechanism to schedule a timed task. The problem is: your web application needs to be running. What if the website isn't called at all? Well, since IIS 7.5 there is the possibility to keep your web app running at all times: auto starting web apps. Jeff suggests in the comments that his approach served well until they outgrew it. His conclusion is that for small sites this is a good approach.

Upvotes: 0

nima
nima

Reputation: 6733

Here's what I did:

   public class Email {
        private static System.Threading.Timer threadingTimer;

        public static void StartTimer()
        {
            if (threadingTimer == null)
                threadingTimer = new Timer(new TimerCallback(Callback), HttpContext.Current, 0, 20000);
        }

        private static void Callback(object sender)
        {
            if (/* Your condition to send emails */)
                using (var context = new MyEntities())
                {
                        var users = from user in context.Usere
                                     select user;
                        foreach (var user in users)
                        {
                            // Send an email to user
                        }
                }
        }
    }

And you have to add this to Application_Start:

    void Application_Start(object sender, EventArgs e)
    {
         EMail.StartTimer();
    }

Upvotes: 0

Saurabh
Saurabh

Reputation: 5727

You can use Window Service to work in backgroud or scheduling , please see below links:

Using Timers in a Windows Service

Upvotes: 0

Chris Fulstow
Chris Fulstow

Reputation: 41902

The Quartz.NET job scheduler library is excellent for this sort of thing.

Upvotes: 2

Related Questions