Click here to Skip to main content
16,022,538 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have created a timer in windows service application and it should execute a method every 1 sec but my problem is the timer works for several seconds and then stop automatically
the service should write a line in the text, that line contains the windows date and time,and the windows time should change every 1 sec
i dunno if the timer is stopped or the whole service is stopped
i need someone to explain why is this happening

protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            System.Timers.Timer T1 = new System.Timers.Timer();
            T1.Interval = (1000);
            T1.AutoReset = true;
            T1.Enabled = true;
            T1.Start();
            T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);
        }
 
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            System.Timers.Timer T1 = new System.Timers.Timer();
            T1.Interval = (1000);
            T1.AutoReset = true;
            T1.Enabled = true;
            T1.Start();
            T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);
        }
        private void T1_Elapsed(object sender, EventArgs e)
        {
            FileStream TS = new FileStream(@"C:\Test.txt", FileMode.OpenOrCreate);
            string lines = "Service has been started at" + DateTime.Now.ToString();
            System.IO.StreamWriter file = new System.IO.StreamWriter(TS);
            file.WriteLine(lines);
            file.Close();
            TS.Close();
        }
Posted
Updated 1-May-11 2:37am
v2

That's a terrible design.

0) Why don't you use the system log for that?

1) Why are you causing so much file I/O? the system is going to be spending so much time writing to disk that it won't have much time for anything else. If you absolutely need to write to the disk every second, buffer the text to be written for a minute or two, and then flush the buffer to the file all at once.

2) What happens if your text file manages to fill the disk?

3) The problem is probably that you're trying to handle the timer event while the last time event is still being processed (see item 1 above).

4) Put a try catch block around the code in the T1_Elapsed method that will simply ignore the event if an exception is thrown.

C#
try
{
   do my processing
}
catch (Exception)
{
}
 
Share this answer
 
v3
Comments
RaviRanjanKr 1-May-11 13:20pm    
My vote of 5 :)
Sergey Alexandrovich Kryukov 1-May-11 18:27pm    
You are right, my 5. (I don't agree with ignoring the exception; as a minimum, it should log the problem).

I added the usual advice to use thread instead of exception, please see my solution.
--SA
You are creating a timer instance both when the service is started and stopped?

You should create the timer in an init method, the constructor would suffice, then start it in the OnStart event handler and stop it in the onStop event handler.

Also, opening and closing FileStream objects is an intensive process, doing it every second won't make your service very performant.

Have you checked the Windows event log for any messages about your service?
 
Share this answer
 
That's a terrible design.

Avoid using timers. This is exactly a case when a timers can make a lot more harm than help.
Instead, create a thread which will repeat the call, use System.Threading.Thread.Sleep — for required amount of time.

Use logging indeed.

—SA
 
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