Solved

Scheduling Windows Service

Posted on 2012-04-04
4
223 Views
Last Modified: 2012-04-05
I have a Windows Service that I need to run every hour but I want it done programmatically, not via Windows Scheduler. Perhaps using a Timer or something.

Can someone tell me the best way I can accomplish that? I will need some code examples too
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 37807351
The following code sample using Timer should meet your needs:
namespace MyApp
{
    public partial class MyAppService : ServiceBase
    {
        private System.Timers.Timer _timer = new System.Timers.Timer();

        public MyAppService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // Service Started
            
            _timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
            
            _timer.Interval = 1000;  // Executes the first run after 1 sec

            _timer.Enabled = true;
        }

        protected override void OnStop()
        {
            _timer.Enabled = false;

            // Service Stopped
        }

        protected void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _timer.Enabled = false;

            ServiceTask();

            _timer.Interval = 60 * 60 * 1000; // 60 mins (60 * 60 * 1000 millisecs)
                                              // Ideally this should come from a configuration file
            _timer.Enabled = true;
        }

        private void ServiceTask()
        {
            try
            {
                // <<<<< Add your service processing code here >>>>>
            }
            catch (Exception ex)
            {
                // Log exception
            }
        }
    }
}

Open in new window

0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 37812137
Exactly what I needed! Thank you
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37812243
I'm glad I could help.  

BTW, a quick note about the code.  Because on line #32 & #38 the timer is Disabled/Enabled the next process run is 1 hour after the last execution completed.  To run the process once every hour the code needs to be a little different as shown below:

namespace MyApp
{
    public partial class MyAppService : ServiceBase
    {
        private const double INTERVAL =  60 * 60 * 1000; // 60 mins (60 * 60 * 1000 millisecs); Ideally this should come from a configuration file

        private System.Timers.Timer _timer = new System.Timers.Timer();

        public MyAppService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // Service Started
            
            _timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
            
            _timer.Interval = 1000;  // Executes the first run after 1 sec

            _timer.Enabled = true;
        }

        protected override void OnStop()
        {
            _timer.Enabled = false;

            // Service Stopped
        }

        protected void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (_timer.Interval != INTERVAL)
                _timer.Interval = INTERVAL; // Reset the timer interval to the regular value

            ServiceTask();
        }

        private void ServiceTask()
        {
            try
            {
                // <<<<< Add your service processing code here >>>>>
            }
            catch (Exception ex)
            {
                // Log exception
            }
        }
    }
}

Open in new window

0
 
LVL 35

Author Comment

by:YZlat
ID: 37812306
Thank you for the clarification.

Could you also help me with this question:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27664066.html
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question