Click here to Skip to main content
13,505,586 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone,

I have a windows service which should run every one hour, but its calling the function at the time of starting and after finishing the work only its says started and again after 1 hour it never call the function.

I put the code below, please suggest

public PosNetService()

        protected override void OnStart(string[] args)


        private void startservice()


        private void checkstatus()

                System.Threading.Thread.Sleep(1000 * 60 * 60);
// then remaining work to do
Posted 4-Feb-13 23:56pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

With System.Timers.Timer you have more options, where System.Threading.Timer is a lightweight timer. I would recommend you to use System.Timers.Timer

Try this ...

using System.Timers;

Timer tmrExecutor = new Timer();

protected override void OnStart(string[] args)
      tmrExecutor.Elapsed += new ElapsedEventHandler(tmrExecutor_Elapsed); // adding Event
      tmrExecutor.Interval = 5000; // Set your time here 
      tmrExecutor.Enabled = true;

private void tmrExecutor_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      //Do your work here 

protected override void OnStop()
      tmrExecutor.Enabled = false;
vaibhav mahajan 28-Sep-16 6:45am
Worked for me. Thanks Sheikh Muhammad Haris.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180417.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100