Solved

Scheduling Windows Service

Posted on 2012-04-04
4
190 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
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
Comment Utility
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
Comment Utility
Exactly what I needed! Thank you
0
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
c# code 19 57
How do I bind the results to a grid 3 22
Data Saving 5 24
help with issues with ReportViewer in VS2015 5 22
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now