Click here to Skip to main content
15,885,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Web Service and i used timers inside it. when i run the service application inside my vs2010 it work's like a charm. but when i deploy it to the remote server it doesn't fire the ElapsedEventHandler. I used a Logger to log the events and i set the permissions for write into the log file. Let me explain more. I'm trying to write a service which waits for a job, and the job is sending email to a big list. and emails should be send by 20 secs interval. here is some part of my code:
C#
public class EmailSender : IEmailSender
{

    Queue<AdvEmail> ListOfEmails = new Queue<AdvEmail>();
    List<EmailJob> EmailJobs = new List<EmailJob>();

    Timer timerFetchJobs = null;
    Timer timerSendEmails = null;

    public EmailSender()
    {
        try
        {
            Logger.Log("init service");
            timerFetchJobs = new Timer();
            timerFetchJobs.Interval = 5 * 1000;
            timerFetchJobs.Elapsed += new ElapsedEventHandler(timerFetchJobs_Elapsed);
            timerFetchJobs.Enabled = true;
            timerSendEmails = new Timer();
            timerSendEmails.Interval = 20 * 1000;
            timerSendEmails.Elapsed += new ElapsedEventHandler(timerSendEmails_Elapsed);
            timerSendEmails.Enabled = true;
            timerFetchJobs.Start();
            timerSendEmails.Start();
            Logger.Log("init service Ended Succefully");
            Logger.Log("timerFetchJobs.Enabled=" + timerFetchJobs.Enabled.ToString());
            Logger.Log("timerSendEmails.Enabled=" + timerSendEmails.Enabled.ToString());
        }
        catch (Exception ex)
        {
            Logger.Log("Error in InitService. Message: " + ex.Message);
        }
    }

    void timerSendEmails_Elapsed(object sender, ElapsedEventArgs e)
    {
        timerSendEmails.Stop();
        Logger.Log("Checking for Emails in the Queue");
        // some statments here

        timerSendEmails.Start();
    }

    void timerFetchJobs_Elapsed(object sender, ElapsedEventArgs e)
    {
        timerFetchJobs.Stop();
        try
        {
            FetchJobsFromDB();
        }
        catch (Exception ex)
        {
            Logger.Log("Error in timerFetchJobs. Message: " + ex.Message);
        }
        timerFetchJobs.Start();
    }

when i go to the service addreess from browser, it initializes and logs the init level correctly, but no clue of firing timers!! which i dont know why?? what I'm doing wrong? thanks

Edit: here is the log file when i run it locally:

Log Entry : 1392/01/30 08:47:09 : :init service-------------------------------

Log Entry : 1392/01/30 08:47:09 : :init service Ended Succefully-------------------------------

Log Entry : 1392/01/30 08:47:09 : :timerFetchJobs.Enabled=True-------------------------------

Log Entry : 1392/01/30 08:47:09 : :timerSendEmails.Enabled=True-------------------------------

Log Entry : 1392/01/30 08:47:14 : :Checking for Jobs in the Database-------------------------------

Log Entry : 1392/01/30 08:47:16 : :There is a new job. job name is Pad 71-------------------------------

Log Entry : 1392/01/30 08:47:19 : :Adding Emails for th job Pad 71-------------------------------

And here is the log file when i deploy it to remote server

Log Entry : 1392/01/30 08:47:09 : :init service-------------------------------

Log Entry : 1392/01/30 08:47:09 : :init service Ended Succefully-------------------------------

Log Entry : 1392/01/30 08:47:09 : :timerFetchJobs.Enabled=True-------------------------------

Log Entry : 1392/01/30 08:47:09 : :timerSendEmails.Enabled=True-------------------------------
Posted

System.Timers.Timer has a AutoReset property which you should set to True.
 
Share this answer
 
Comments
mahdi87_gh 20-Apr-13 13:06pm    
thanks buddy. i did that but no change. I'm wondering why it doesn't work. it works perfectly on my vs2010. and another point is, it must run at least for one time!!! and i get the "Checking for Jobs in the Database" log. am i right?
I changed logger class to log in a string variable and add a operation contract to get the log and the problem gone!!!

now i have another problem. after a while the timers stop!! seems when there is no new request, IIS shuts down the service! how can i have a infinitive time service?
 
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