stopping windows service.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • archana

    #1

    stopping windows service.

    Hi all,

    I am having one windows service which is updating to database.

    On 'Onstop i want to wait till current updation complete. How will i
    do this?

    Because if i write some lengthy code on onstop it fails to stop and
    status remain 'stopping' Later i can't do anything on that service
    even i can't uninstall that service.

    Please tell me solution for this.

    Please help me asap.

    thanks in advance.

  • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

    #2
    RE: stopping windows service.

    I believe that once you are in OnStop you've invoked the Service Control
    Manager and if she can't stop your service in the alloted time, she will blow
    up.

    You could check if the operation is complete and bail out if it isn't. Other
    folks do stuff like this:

    private void InvokeOnStop()
    {
    ServiceControll er sc = new ServiceControll er(this.Service Name);

    sc.Stop();
    sc.Dispose();
    }

    -- Although current wisdom indicates that it isn't a good idea.

    Peter
    --
    Recursion: see Recursion
    site: http://www.eggheadcafe.com
    unBlog: http://petesbloggerama.blogspot.com
    bogMetaFinder: http://www.blogmetafinder.com



    "archana" wrote:
    Hi all,
    >
    I am having one windows service which is updating to database.
    >
    On 'Onstop i want to wait till current updation complete. How will i
    do this?
    >
    Because if i write some lengthy code on onstop it fails to stop and
    status remain 'stopping' Later i can't do anything on that service
    even i can't uninstall that service.
    >
    Please tell me solution for this.
    >
    Please help me asap.
    >
    thanks in advance.
    >
    >

    Comment

    • archana

      #3
      Re: stopping windows service.

      Hi,

      thanks for your reply.

      But how will i wait for some operation to complete on 'onstop event'.
      See like in windows service i can forece user to wait for some
      operation to complete by using waitall. That won't cause any
      problem. Like that what i need to do in windows service to wait for
      current operation to complete.

      if u can shed some light on it then it will be really helpful for me,

      thanks.

      Comment

      • Mark Rae [MVP]

        #4
        Re: stopping windows service.

        "archana" <trialproduct20 04@yahoo.comwro te in message
        news:1185796411 .208101.12770@d 55g2000hsg.goog legroups.com...
        But how will i wait for some operation to complete on 'onstop event'.
        See like in windows service i can forece user to wait for some
        operation to complete by using waitall. That won't cause any
        problem. Like that what i need to do in windows service to wait for
        current operation to complete.
        >
        if u can shed some light on it then it will be really helpful for me,
        I usually set a boolean flag true when a process starts and then set it
        false when the process ends.

        In the OnStop event, I check the value of the flag and wait until it is
        false before shutting down the service.


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • Mr. Arnold

          #5
          Re: stopping windows service.


          "archana" <trialproduct20 04@yahoo.comwro te in message
          news:1185793903 .282923.254210@ d55g2000hsg.goo glegroups.com.. .
          Hi all,
          >
          I am having one windows service which is updating to database.
          >
          On 'Onstop i want to wait till current updation complete. How will i
          do this?
          >
          Because if i write some lengthy code on onstop it fails to stop and
          status remain 'stopping' Later i can't do anything on that service
          even i can't uninstall that service.
          >
          Please tell me solution for this.
          >
          Please help me asap.
          When you execute the code each time for the update, you set a global Boolean
          flag at the top of the routine to *true* like blnUpdating and set the flag
          to false when the routine is exited.

          Then in your OnStop you will have code like this.

          While blnUpdating
          {
          make it wait one second by using System.ThreadTi mer.Sleep(wait time)
          }

          It will stay in the While loop as long as blnUpdating = true.
          It will come out of the While loop when blnUpdating is set to false when the
          Update routine has exited.

          The code in the OnStop will be executed after the loop to stop the service.

          The Update routine may need to be running on a child thread while the Onstop
          is on the parent thread the thread the service started up on.

          You may also want to look at the service's 'CanStop' property by setting it
          to true or false as well, but I have never used that property to prevent the
          service from stopping.



          Comment

          • archana

            #6
            Re: stopping windows service.

            Hi all,

            thanks for replies.

            I used same thing. I am getting following error

            Could not stop the 'MyTestService' service on Local Computer.

            Error 1053: The service did not respond to the start or
            control request in a timely fashion.

            Can anyone tell what to do to solve this problem. Becase its major
            problem and on service i can't restart server everytime if this error
            occurs. And i want proper way to stop service.

            thanks.

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: stopping windows service.

              When a service is told to stop, I believe it has 30 seconds before the
              Service Control Manager abandons the service.

              What you need to do is call the SetServiceStatu s API function through
              the P/Invoke layer, using the value returned from the ServiceHandle property
              (it is the handle to the service which you can pass to the SetServiceStatu s
              function).

              In your OnStop method, you would have to wait a little bit, check to see
              if the update completed, then call SetServiceStatu s, indicating that you are
              waiting (incrementing the dwCheckPoint field of the SERVICE_STATUS
              structure, so that your service knows you are doing something).

              I can't recall completely, but I think there is an absolute value that
              windows will wait for the service to wait, even in light of the calls to
              SetServiceStatu s, which if exceeded, will cause the service to be
              terminated, so you might want to make sure your update completes in a timely
              manner in this case.


              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m

              "archana" <trialproduct20 04@yahoo.comwro te in message
              news:1185804987 .311161.145350@ l70g2000hse.goo glegroups.com.. .
              Hi all,
              >
              thanks for replies.
              >
              I used same thing. I am getting following error
              >
              Could not stop the 'MyTestService' service on Local Computer.
              >
              Error 1053: The service did not respond to the start or
              control request in a timely fashion.
              >
              Can anyone tell what to do to solve this problem. Becase its major
              problem and on service i can't restart server everytime if this error
              occurs. And i want proper way to stop service.
              >
              thanks.
              >

              Comment

              • Mr. Arnold

                #8
                Re: stopping windows service.


                "archana" <trialproduct20 04@yahoo.comwro te in message
                news:1185804987 .311161.145350@ l70g2000hse.goo glegroups.com.. .
                Hi all,
                >
                thanks for replies.
                >
                I used same thing. I am getting following error
                >
                Could not stop the 'MyTestService' service on Local Computer.
                >
                Error 1053: The service did not respond to the start or
                control request in a timely fashion.
                >
                Can anyone tell what to do to solve this problem. Becase its major
                problem and on service i can't restart server everytime if this error
                occurs. And i want proper way to stop service.
                I think the service has blown up somewhere or at some point it went into a
                loop somewhere and didn't come out, which in either case it's causing the
                Onstop to timeout. You need to debug your service.

                Comment

                • archana

                  #9
                  Re: stopping windows service.

                  Hi,

                  thanks for your reply.

                  can u tell me how to do this. Because i searched on net to get
                  information about it to implement in framework 1.1. But of no luck.

                  please help me.

                  thanks

                  Comment

                  • Nicholas Paldino [.NET/C# MVP]

                    #10
                    Re: stopping windows service.

                    I kind of did already in my previous post? The SetServiceStatu s API is
                    a windows API function, and you have to call it through the P/Invoke layer.
                    You can get the definition from http://www.pinvoke.net most likely. You can
                    also most likely get the definition of the SERVICE_STATUS structure from
                    there as well.

                    The ServiceHandle property is not available in .NET 1.1, so you will
                    have to resort to reflection on the ServiceBase instance (the class you
                    derive from) to get the value. It would be an implementation detail, but
                    when you migrate beyond 1.1, you know it will be supported (it's a bit of a
                    hack, at the least in 1.1, but from what I can tell, the only way to get
                    what you want).


                    --
                    - Nicholas Paldino [.NET/C# MVP]
                    - mvp@spam.guard. caspershouse.co m

                    "archana" <trialproduct20 04@yahoo.comwro te in message
                    news:1185871630 .597050.318670@ w3g2000hsg.goog legroups.com...
                    Hi,
                    >
                    thanks for your reply.
                    >
                    can u tell me how to do this. Because i searched on net to get
                    information about it to implement in framework 1.1. But of no luck.
                    >
                    please help me.
                    >
                    thanks
                    >

                    Comment

                    • Shilpa Ginode

                      #11
                      Re: stopping windows service.



                      Hi archana,

                      I saw ur thread regarding stopping the windows service.

                      Even I am facing the same problem.

                      I have created a thread in the OnStart() event of the windows service
                      and calling the method of the class library project(.dll).
                      I put the thread to sleep in the method for some period of time.(1 min)
                      On the OnStop() event I want to stop the service only after completion
                      of the work by the thread.(not in the middle.)

                      Did you get any solution for your problem?
                      If yes can you please share with me that or provide me the sample code
                      for the same?

                      Thanx in advance.

                      *** Sent via Developersdex http://www.developersdex.com ***

                      Comment

                      Working...