Click here to Skip to main content
14,920,659 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
i have just created my first windows service application and i'm not able to use a timer in this case , if it's available to use a timer in windows service,i need someone to post a code and explain how it works.

Thanks

Hello All
Thanks for your answers,i have created the timer and it should execute a method every 1 sec but my problem is the timer works for several seconds and then stop automatically
i need someone to explain why this is 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 = (10000);
            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 = (10000);
            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();
        }


i'm waiting your answers
Posted
Updated 28-Feb-21 22:34pm
v3

Take a look at System.Timers.Timer[^] or System.Threading.Timer[^]

With System.Timers.Timer you have more options, where System.Threading.Timer is a lightweight timer.
   
v2
You can use a windows timer, just not System.Web.UI.Timer or System.Windows.Forms.Timer

Both System.Timers.Timer and System.Threading.Timer will work for services, but you might want to consider a console app (with no window) and Windows Scheduler instead - it depends on what you are doing with your service.

There is a discussion with code examples for both here: http://stackoverflow.com/questions/246697/windows-service-and-timer[^]
   
The timer approach is the most common method and is probably the simplest to write and understand. You create a timer in the OnStart event and attach your worker function to the timer.
Take a look there[^] for detailed description.
   
protected override void OnStart(string[] args)
       {

           // TODO: Add code here to start your service.
           Timer T1 = new Timer();
           T1.Interval = (1000);
           T1.AutoReset = true;
           T1.Enabled = true;
           T1.Start();
           T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);



           if (!File.Exists(@"F:\KillLog.txt"))
           {
               File.Create(@"F:\KillLog.txt");
           }
           using (StreamWriter sw = new StreamWriter(@"F:\KillLog.txt", true))
           {
               sw.WriteLine("Killing process Service starts at : {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));

           }
           timer1.Enabled = true;
       }

       protected override void OnStop()
       {
           // TODO: Add code here to perform any tear-down necessary to stop your service.
           Timer T1 = new Timer();
           T1.Interval = (1000);
           T1.AutoReset = true;
           T1.Enabled = true;
           T1.Start();
           T1.Elapsed += new System.Timers.ElapsedEventHandler(T1_Elapsed);
           using (StreamWriter sw = new StreamWriter(@"F:\KillLog.txt", true))
           {
               sw.WriteLine("Killing process Service stops : {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));

           }
           timer1.Enabled = false;
       }



       private void timer1_Tick(object sender, EventArgs e)
       {

           using (StreamWriter sw = new StreamWriter(@"F:\KillLog.txt", true))
           {
               sw.WriteLine("Checking Process: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));

           }
           Process[] runingProcess = Process.GetProcesses();
           for (int i = 0; i < runingProcess.Length; i++)
           {
               //Console.WriteLine(runingProcess[i].ProcessName);

               // compare equivalent process by their name
               if (runingProcess[i].ProcessName == "mspaint")
               {
                   // kill  running process
                   runingProcess[i].Kill();
               }

           }

       }
       private void T1_Elapsed(object sender, EventArgs e)
       {
           using (StreamWriter sw = new StreamWriter(@"F:\KillLog.txt", true))
           {
               sw.WriteLine("Checking Process: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));

           }
           Process[] runingProcess = Process.GetProcesses();
           for (int i = 0; i < runingProcess.Length; i++)
           {
               //Console.WriteLine(runingProcess[i].ProcessName);

               // compare equivalent process by their name
               if (runingProcess[i].ProcessName == "mspaint")
               {
                   // kill  running process
                   runingProcess[i].Kill();
               }

           }
       }
   
Comments
Ralf Meier 1-Mar-21 6:01am
   
of course you have seen that this question is nearly 10 years old ...?

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