• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 725
  • Last Modified:

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

0
Number5ix
Asked:
Number5ix
  • 3
  • 2
1 Solution
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now