Suspend a service at a given time during the day

Posted on 2008-11-11
Last Modified: 2012-05-05
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?
Question by:vbhargav80
    LVL 14

    Accepted Solution

    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>
    LVL 23

    Expert Comment

    Do you simply want to stop, then restart it, or pause it

    Author Comment

    Thanks everyone.
    LVL 2

    Expert Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One C# Help from Top Experts

    Solve your toughest problems, fast.
    C# experts are online now and ready to help you.

    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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now