Click here to Skip to main content
11,576,692 members (58,764 online)
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 4-Feb-13 23:56pm

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 251
1 Abhinav S 135
2 Lucifier Rocks 105
3 Dave Kreskowiak 80
4 F-ES Sitecore 80
0 OriginalGriff 705
1 Sergey Alexandrovich Kryukov 626
2 Abhinav S 483
3 F-ES Sitecore 410
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web03 | 2.8.150603.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