• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Windows Service Timed Action

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Windows Service Timed Action

    Easy one for you .netters.

    I have written a windows service in C#2010, compiled to run on x86 using 3.5.

    I put all my mission critical stuff in a lib and unit tested it thoroughly.

    Being a windows service, if it encounters a problem I wanted it to log to the windows event log.

    All works like a charm, kindof.

    Put simply, my service starts up, loads it's settings, reports to the windows event log that it is up.

    Then it kicks off a System.Timer event for 60s hence.

    This fires ok.

    Then in the event I say something like

    DoFunctionA();
    LogEvent("I did a");

    DoFunctionB();
    LogEvent("I did b");

    DoFunctionC();
    LogEvent("I did c");

    DoFunctionD();
    LogEvent("I did d");

    Everything has proper error handling and the catch blocks also log to the event log.

    The output to the event log is :

    I'm Up
    I did A
    I did B
    I did C

    .....

    Just stops at C. The only logical place for the code to go is to exception, which is logged, or on to task D. Is does neither.

    Run the windows service as a console app and it works like a trooper.

    Alls I can think is that the tasks a,b,c and d are quite memory intenstive and laborious. Is there a known issues with System.Timers not liking heavy threads?

    Any experince from the panel muchly appreciated.

    Ta

    In awe

    Suity
    Knock first as I might be balancing my chakras.

    #2
    Does the process actually end or have you got loads of instances running from your tests... and what's the situation with trying to attach a debugger to a service?

    What is D() doing - is it all C# code or any unmanaged stuff?
    Originally posted by MaryPoppins
    I'd still not breastfeed a nazi
    Originally posted by vetran
    Urine is quite nourishing

    Comment


      #3
      What happens if you do D first?
      Will work inside IR35. Or for food.

      Comment


        #4
        Litter your code with debug logging or attach VS debugger to the service and step through it.

        Comment


          #5
          Originally posted by d000hg View Post
          Does the process actually end or have you got loads of instances running from your tests... and what's the situation with trying to attach a debugger to a service?

          What is D() doing - is it all C# code or any unmanaged stuff?
          Doesn't really matter what D is doing. If D was doing something naughty then it would go to exception handling and log to the event log. It is all C# managed code.

          One service, one thread.

          I have a natty way of compiling it as a console exe, yet it still installs as a service so you can run as console or as service.

          It would have to be running long enough to attach a debugger I guess, plus the server it is running on is in NY and the connection from my machine is laggy.

          I think the question here is why does the thread mysteriously stop executing when it's run as a service, and yet work perfectly when run as a console app.

          The security context for both is the same.
          Knock first as I might be balancing my chakras.

          Comment


            #6
            Originally posted by VectraMan View Post
            What happens if you do D first?
            Sadly can't. The outputs of ABC are vital.
            Knock first as I might be balancing my chakras.

            Comment


              #7
              Originally posted by suityou01 View Post
              Sadly can't. The outputs of ABC are vital.
              Can you fake the outputs of ABC so that it doesn't have to do it?

              How does C# handle exceptions within exception handling? In C++ it can cause the whole process to abort...

              I think you probably need to break down D() with lots of logging to work out where it stops.
              Will work inside IR35. Or for food.

              Comment


                #8
                is it doing something that takes longer than 60 secs and a second run kicks off and gets in the way?

                when I get something this screwy, I usually use debug lines like others suggested to make sure I'm absolutely sure of the program flow.

                Comment


                  #9
                  Originally posted by VectraMan View Post
                  Can you fake the outputs of ABC so that it doesn't have to do it?

                  How does C# handle exceptions within exception handling? In C++ it can cause the whole process to abort...

                  I think you probably need to break down D() with lots of logging to work out where it stops.
                  Glad to have the attention of the big guns.

                  I also know C++ so know what you mean. In C Hash land, we are bottle fed. The code is called "Managed code", which means as long as you play nice and only use .net functions all objects are garbage collected and it's all nice. The moment you make an API call, say, then you are stepping outside managed code and there be dragons.

                  This is all managed code. There is no place to roam other than the next instruction, or the exception handler. To just stop like this is most odd.

                  Originally posted by jmo21 View Post
                  is it doing something that takes longer than 60 secs and a second run kicks off and gets in the way?

                  when I get something this screwy, I usually use debug lines like others suggested to make sure I'm absolutely sure of the program flow.
                  Great question. The first order of business in my tick event is to reset the timer to prevent precisely this. The idea is the service will kick off every morning at 7am, unless you just started it in which case do it in 60s from starting the service. Obviously I want the on start event to do the bare minimum so it just grabs the necessary settings for the mission, instantiates the timer and sets that up then finishes.

                  Then the timer fires and we move into the twighlight zone.
                  Knock first as I might be balancing my chakras.

                  Comment


                    #10
                    Originally posted by VectraMan View Post
                    Can you fake the outputs of ABC so that it doesn't have to do it?

                    How does C# handle exceptions within exception handling? In C++ it can cause the whole process to abort...
                    Originally posted by suityou01 View Post
                    Glad to have the attention of the big guns.

                    I also know C++ so know what you mean. In C Hash land, we are bottle fed. The code is called "Managed code", which means as long as you play nice and only use .net functions all objects are garbage collected and it's all nice. The moment you make an API call, say, then you are stepping outside managed code and there be dragons.

                    This is all managed code. There is no place to roam other than the next instruction, or the exception handler. To just stop like this is most odd.
                    You do realise this is why I asked what D is doing, specifically if it involved unmanaged code
                    Originally posted by MaryPoppins
                    I'd still not breastfeed a nazi
                    Originally posted by vetran
                    Urine is quite nourishing

                    Comment

                    Working...
                    X