Solved

Scheduling Windows Service

Posted on 2012-04-04
4
226 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net/VSTO Excel Add-in 2 36
c#, datatable, aspx 4 48
asp.net repeater 2 34
VB .net 2010 Byte array 2 19
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

737 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