Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using global.asax to send mail at regular interval. The time to send mail is set in db and can be any time in 24 hours(like 1 am, 1 pm, 6 am and so on). This is my code :

C#
void Application_Start(object sender, EventArgs e)
   {
       System.Threading.Timer _timer = new System.Threading.Timer(new System.Threading.TimerCallback(GetSchedule));
       _timer.Change(0, (1000 * 60 * 60));
   }


C#
void GetSchedule(Object state)
   {
       try
       {
           lock (state)
           {
               EmailingReportToCustomers.GetDBAndReportTypeOfCustomers();
           }
       }
       catch (Exception ex)
       {

       }
   }



C#
public static void GetDBAndReportTypeOfCustomers()
   {

//Queries db for time in table. If time now is db time. Then SendMail()
}

The mail is send at given time as needed. The problem I am facing here is same mail is being sent multiple times (8 times). I think GetDBAndReportTypeOfCustomers() is being queried multiple times.

What I have tried:

Initially there was no _timer.Change(0, (1000 * 60 * 60));. After some research, I added timer as said my them to control multiple sending of mail. But it is not working, Any help ???
Posted
Updated 3-Oct-16 2:42am
Comments
[no name] 3-Oct-16 8:37am    
"I think", you think? Debug your application and you would *know*.
"But it is not working", we have no idea what you think "not working" means. Why are you not using the proper tool for this email processing?
Codes DeCodes 3-Oct-16 23:42pm    
I do not want to use window services. I am not able to correctly debug. In my debugguing, codes executes only once. But on server when time triggers, it is creating an issue..
Karthik_Mahalingam 3-Oct-16 11:58am    
Always use  Reply   button to post comments/query to the concerned user, so that the user gets notified and respond to your text.
F-ES Sitecore 3-Oct-16 9:04am    
Use something like Quartz.net rather than trying to develop your own.

1 solution

First, using a Timer is not really a good idea. It binds all the email sending code to your site being up and running. Also, it doesn't scale across servers. You'll have more than one server running the email check at different times. That's not the job of your web server.

It's POSSIBLE to do, just not a good idea.

I'd have a separate Windows service that runs the email send job. Now, how do you know if the email was sent already? You have to track that information in your database somehow. Like setting a flag that may say "this user needs an email sent" and/or "this user has NOT has an email sent", but since you're doing this on a schedule, it would also have to say "this user has NOT had an email sent to them at such and such time". How you design that is up to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900