Schedules a Timer in a Website : C#,asp.net Automation Method

by daz 9/22/2009 3:58:00 AM

I had a project that consist of opening an Emailbox, read all the Spam and get the emails(from email of course), other parameters... and insert them in a Sql Database Table. I have a Button on a page where the admin of the site will go and click to start the reading. Problem: when it got to 350 emails, u got a Page Time out. Solution: Create a Timer for every 30 min , read the mail box. Now get rid of the Admin pain!! I will demonstrate how this can be done

To first start make sure you have the Global.asax file in your Project. I am using asp.net 3.5 and Linq to Sql for this particular Website I create a file SpamReader.cs inside of my Utils Project.

public class SpamReader {
public SpamReader()
{
}
public void _timer_Elapsed(object sender, ElapsedEventArgs e) {
ReadSpamComplaint();
}
public void ReadSpamComplaint()
{
string sServer = "pop.mysite.com";
string sUserName = "username";
string sPassword = "password";
GetMyMails(sServer, sUserName, sPassword, false);
}
private static void GetMyMails(string sServer, string sUserName, string sPassword, bool bSSLConnection)
{
}
}

It is about it. Hold on, one more thing to do. Now it is time to use the Timer class. Very easy...Open your Global.asax.cs and in Application start call the Spamreader class..


protected void Application_Start(object sender, EventArgs e)
{
SpamReader spam = new SpamReader();
Timer zTimer = new Timer();
zTimer.Elapsed += new ElapsedEventHandler(spam._timer_Elapsed);
zTimer.Interval = 900000;//every 15 min
zTimer.Start();
}

and set the timer to what ever you want. my case 15min=15x60minx60sec and add 000 for the Miliseconds.. That 's about it. I hope you enjoyed it. Leave a comment please.

About the author

Name of author
DavidZ
.Net Programmer
E-mail me Send mail
WebHosting Canada USA
Kaspersky Anti-Virus 2011
Flightnetwork.com -Specializing in Cheap Flights F

Partners

Repertoire canada

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in