Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,534
1 OriginalGriff 6,258
2 Peter Leow 2,534
3 Maciej Los 2,308
4 Abhinav S 2,274


Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2015
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