Solved

Scheduling Windows Service

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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