windows service running on particular time and debug.write() where it prints ?

Dear All,

I have windows service done with C# and able to be install using the installutil.exe. My problem now is that I want to set it like to run on particular time of day for instance 10.00 am daily How can I set this I have tried to use timer but it sets an interval only Another thing is that I have debug.write() in my windows service where can i check the output of the debug.write Where is the file Thanks.



Answer this question

windows service running on particular time and debug.write() where it prints ?

  • Ilia

    Dear Galin,

    I got your idea. The problem now I need to confirm with you is that am i writing my action codes in the write place or not When I create a new windows service program with VS2005 I get only 2 function OnStart and OnStop. So I am putting my code in the OnStart. So it this correct to do that or not Actually once I have installed and started the service it gets start to it will call the OnStart function rite So after that where does it go


  • MightilyOats

    1. I prefer detecting in code if it is appropriate time to perform actions. thus way you eliminate chance to have service stopped at the time it must work.

    in your code you can check if it is appropriate time with similar code

    //code inside if statement is executed only beteween 10 AM and 6 PM

    //get now
    DateTime now = DateTime.Now;

    if (10 <= now.Hour && now.Hour <= 18){
    //your code here executed every day beteween 10 AM and 6 PM
    }

    2. Debug and Trace classes write in all listeners

    hope this helps



  • Kelly R. Martin

    as I understand you have to.

    1. declare timer variable in class body.
    2. in OnStart method initialize Timer variable and add event handler to Elapsed event.
    3. Make timer enabled In this event hlandler do your work as you check for the time constraints
    4.  In OnStop method disable timer.

    See the code below:

    public partial class Service1 : ServiceBase
    {
       
    Timer timer = null;

       public Service1(){
            InitializeComponent();
        }

        protected override void OnStart(string[] args){
            timer = new Timer();

            timer.Interval = 2000;
    //two seconds
           
    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Enabled =
    true;
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e){
           
    //get now
            
    DateTime now = DateTime.Now;
           
    if (10 <= now.Hour && now.Hour <= 18){
                    
    //your code here
            
    }
         }

        protected override void OnPause(){
            
    base.OnPause();
             timer.Enabled =
    false;
        }

         protected override void OnContinue(){
            
    base.OnContinue();
             timer.Enabled =
    true;
         }

        protected override void OnStop(){
            timer.Enabled =
    false;
            timer.Dispose();
         }
    }

     

    Hope this helps



  • Don McDonald

    Dear Galin,

    Thanks the website was very informative on service class. But to be frank I am very new and I saw even the c# example code attached together in the web site. I also understand that in my case the operation is on iterative basis. So basically the main engine which suppose to run it should be located in the onStart function am i right I saw the code and trying to understand and implement for my program. What is the worker thread I want to run my service between a range of time at a particular interval If you have any other reference please do help me. Thanks.


  • guilhermecvm94558

    to be honest I haven't dug deeper into these timers business and I cannot adivse you.

    Take a look at these articles. I hope you will find the answer:

    Hope this helps

    UPDATE: I looked at Reflector and System.Timers.Timer acts as wrapper arround System.Threading.Timer



  • barkingdog

    Dear Gallin,

    1. Which you think is better I think a pair of schedule task will be good one for start and stop the service Do you have any example code how to start and stop the service plsease. Any example on the code against time also will be valuable to me.

    2. Ok about the debug.write infact in my windows service I put a line. Now I am wondering where will the output of the debug.write can be seen Thanks for your help.


  • Cactus77

    Well this is question about windows service lyfecycle :)

    • OnStart is called when service is started from service controller and inside it you should initialize your variables and prepare serivice to be ready (to do ... see bellow )
    • OnStop - clear all resources
    • OnPause - mark servce as inactive so your custom method are not executed - e.g disable Timer
    • OnContinue - mark servce as active so your custom method are executed - e.g enable Timer

    For more info take a look at ServiceBase class.

    Window service performs operations on two basis:

    1. Iterative - with timer you regulary check if you have to do something - by using timer and handle it's Tick event
    2. Event - you wait for event and do something - by hosting Remote server object

    Which one you get depends only on your goals (which I still do not know so I cannot give advise)

    Hope this helps.



  • Music Mogul

    Dear Galin,

    I read in some site in the net saying that thread timer is more secure than timer So what is this type of timer is it different than the one you suggesting to me Thanks.


  • Shan Ganesh

    1. About running service on particular time of day you have two options:

    • check in code against time variables (probably set in registry) and just do nothing if you are outside this interval
    • add pair of scheduled tasks - one for start service and one for end it.

    2. Debug.MethodNAme works only if assembly is compiled in Debug configuration. Consider using Trace Class with TextWriterTraceListener Class (System.Diagnostics)

    Hope this helps



  • windows service running on particular time and debug.write() where it prints ?