Suspend a service at a given time during the day

Dear experts,

I have a windows service that runs continuously reading messages from an MSMQ.

Is there a way I can automatically suspend the service for 2 hours between 4 and 6 am?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Yea, just use the Task Scheduler to Stop the service at 4AM and then create another Task to start the service again.

NET START <servicename>


NET STOP <servicename>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Do you simply want to stop, then restart it, or pause it
vbhargav80Author Commented:
Thanks everyone.
To do this from within pure C#, you can do the following code snip.  Add a reference to System.ServiceProcess to your project to use the code snip.
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            //Create a instance of ServiceController looking for our service name
            System.ServiceProcess.ServiceController myservc = new ServiceController("DHCP Client");
            DateTime start, stop;
            while (true)
                start = DateTime.Parse("4:00 PM");
                stop = DateTime.Parse("6:00 PM");
                //Write status to console
                Console.WriteLine("Status: " + myservc.Status.ToString());
                if (start < DateTime.Now && stop > DateTime.Now)
                    if (myservc.Status == ServiceControllerStatus.Running)
                    if (myservc.Status == ServiceControllerStatus.Stopped)
                //Wait 5 seconds, check again.

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.