Why won't my timers fire in my Windows service?

Hi all,

I know that in Windows services you need to use System.Timers.Timer instead of System.Windows.Forms.Timer - no problem there.  In my OnStart method the code runs because I have debug code in there to write to a text file.

However, the same debug code won't write to a text when it's in the Elapsed event of the timer control.

Why???  The main program code is below - I don't think things can get much more simple than this.

I've searched on EE and all I can find is people solving the exact same issue by changing from System.Windows.Forms.Timer to System.Timers.Timer ... I've already done that.

Thanks
namespace CheckUrlResponseService
{
    public partial class CheckWebUpload : ServiceBase
    {
        public CheckWebUpload()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            CheckUrl.Interval = 3000;
            CheckUrl.Enabled = true;
        }
 
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            CheckUrl.Enabled = false;
        }
 
        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            StreamWriter SW = File.AppendText(@"c:\temp\temp.txt");
            SW.WriteLine("hello");
            SW.Close();
        }
    }
}

Open in new window

LVL 3
Number5ixAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
can you share the entire timer code as i cannot see the declaration of timer1
0
philipjonathanCommented:
Did you wired the Elapsed event to the correct event handler?
0
Number5ixAuthor Commented:
raqi0017: Do you mean this?

namespace CheckUrlResponseService
{
    partial class CheckWebUpload
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.CheckUrl = new System.Timers.Timer();
            ((System.ComponentModel.ISupportInitialize)(this.CheckUrl)).BeginInit();
            //
            // CheckUrl
            //
            this.CheckUrl.Interval = 3000;
            //
            // CheckWebUpload
            //
            this.ServiceName = "Check Web Upload";
            ((System.ComponentModel.ISupportInitialize)(this.CheckUrl)).EndInit();

        }

        #endregion

        private System.Timers.Timer CheckUrl;

    }
}

philipjonathan: Yes, I think so but please let me know if you spot any errors in the above code as there's a good chance I missed something.
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

philipjonathanCommented:
I was curious because in your OnStart and OnStop, the timer variable is named CheckUrl, but the event handler is named timer1_Elapsed. Typically if the event handler is generated automatically by Visual Studio, it will be named according to the variable name, like CheckUrl_Elapsed. That's why I ask you to check if the event is wired propertly.

Anyway, try to add event handler after you initialise the timer:

private void InitializeComponent()
{
   this.CheckUrl = new System.Timers.Timer();

  // add this line
   this.CheckUrl.Elapsed += new System.Timer.ElapsedEventHandler(timer1_Elapsed);

  ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Number5ixAuthor Commented:
Complete except for minor typo i.e. should be System.Timers.ElapsedEventHandler instead of System.Timer.ElapsedEventHandler
0
Number5ixAuthor Commented:
Perfect except for the small typo mentioned in the comments - thanks.  :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.