Click here to Skip to main content
13,250,453 members (47,953 online)
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()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {
 
            startservice();
        }
 

        private void startservice()
        {
            checkstatus();
 
        }
 
        private void checkstatus()
        {
 
            try
            {
                System.Threading.Thread.Sleep(1000 * 60 * 60);
// then remaining work to do
Posted 5-Feb-13 0:56am

1 solution

Rate this: bad
 
good
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;
      tmrExecutor.Start();
}
 
private void tmrExecutor_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
      //Do your work here 
}
 
protected override void OnStop()
{
      tmrExecutor.Enabled = false;
}
 
  Permalink  
Comments
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 |
Web04 | 2.8.171114.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2017
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