Self terminating windows service

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

    Self terminating windows service

    Hi All,

    I wrote a windows service which is supposed to stop after specified amount
    of time. I am calling OnStop() after specified time. OnStop() methods
    executed but I dont see the service stopping.

    Please advise how to stop the service.

    Thanks,
    SP


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Self terminating windows service

    SP,

    I don't think that this is a good idea, since the idea of a service is
    that it is supposed to be always running.

    If you have a need for a process to run at a certain time, then I would
    recommend creating an executable and setting it up as a scheduled task which
    runs at a certain time, and then will shut itself down after being run for a
    certain amount of time.

    If you must use a service, then use the ServiceControll er class to get
    the service and shut it down.

    Hope this helps.

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


    "SP" <EAI@sp.com> wrote in message
    news:eyLVy3g3EH A.536@TK2MSFTNG P10.phx.gbl...[color=blue]
    > Hi All,
    >
    > I wrote a windows service which is supposed to stop after specified amount
    > of time. I am calling OnStop() after specified time. OnStop() methods
    > executed but I dont see the service stopping.
    >
    > Please advise how to stop the service.
    >
    > Thanks,
    > SP
    >
    >[/color]


    Comment

    • SP

      #3
      Re: Self terminating windows service

      Hi Nicholas,

      Thank you for your reply. Let me give a brief background about what I am
      trying to achieve.

      As a part of framework I am working on, I have a console application and a
      Windows service. Console application uses windows service. During execution
      console application calls windows service multiple times. I used
      ServiceControll er to start and stop. This part is running fine.

      The issue is, I made windows service a stand alone components which can be
      executed from services console. Now the task of this service is to generate
      a csv file and keep on adding records to it till it is stopped. So just to
      make sure that windows service does not keeps on adding records and exhaust
      all hard disk space because of human error, I wanted to limit the number of
      lines added by the service. Once the specified number of lines are added, I
      want to terminate the service. As of now I am limiting the number of lines.
      But the service itself is not stopping.

      Please let me know whether there is anyway we can do that.

      Thanks,

      SP

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:uhf2$$g3EH A.3908@TK2MSFTN GP12.phx.gbl...[color=blue]
      > SP,
      >
      > I don't think that this is a good idea, since the idea of a service is
      > that it is supposed to be always running.
      >
      > If you have a need for a process to run at a certain time, then I[/color]
      would[color=blue]
      > recommend creating an executable and setting it up as a scheduled task[/color]
      which[color=blue]
      > runs at a certain time, and then will shut itself down after being run for[/color]
      a[color=blue]
      > certain amount of time.
      >
      > If you must use a service, then use the ServiceControll er class to get
      > the service and shut it down.
      >
      > Hope this helps.
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      >
      > "SP" <EAI@sp.com> wrote in message
      > news:eyLVy3g3EH A.536@TK2MSFTNG P10.phx.gbl...[color=green]
      > > Hi All,
      > >
      > > I wrote a windows service which is supposed to stop after specified[/color][/color]
      amount[color=blue][color=green]
      > > of time. I am calling OnStop() after specified time. OnStop() methods
      > > executed but I dont see the service stopping.
      > >
      > > Please advise how to stop the service.
      > >
      > > Thanks,
      > > SP
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Self terminating windows service

        SP,

        It sounds like you basically want shared access to a file from multiple
        sources, right?

        A service IMO is massive overkill for this.

        Rather, you should use a Mutex in a class to block access to other
        processes trying to write to the file.

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

        "SP" <EAI@sp.com> wrote in message
        news:OioVcIh3EH A.708@TK2MSFTNG P11.phx.gbl...[color=blue]
        > Hi Nicholas,
        >
        > Thank you for your reply. Let me give a brief background about what I am
        > trying to achieve.
        >
        > As a part of framework I am working on, I have a console application and a
        > Windows service. Console application uses windows service. During
        > execution
        > console application calls windows service multiple times. I used
        > ServiceControll er to start and stop. This part is running fine.
        >
        > The issue is, I made windows service a stand alone components which can be
        > executed from services console. Now the task of this service is to
        > generate
        > a csv file and keep on adding records to it till it is stopped. So just to
        > make sure that windows service does not keeps on adding records and
        > exhaust
        > all hard disk space because of human error, I wanted to limit the number
        > of
        > lines added by the service. Once the specified number of lines are added,
        > I
        > want to terminate the service. As of now I am limiting the number of
        > lines.
        > But the service itself is not stopping.
        >
        > Please let me know whether there is anyway we can do that.
        >
        > Thanks,
        >
        > SP
        >
        > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
        > in
        > message news:uhf2$$g3EH A.3908@TK2MSFTN GP12.phx.gbl...[color=green]
        >> SP,
        >>
        >> I don't think that this is a good idea, since the idea of a service
        >> is
        >> that it is supposed to be always running.
        >>
        >> If you have a need for a process to run at a certain time, then I[/color]
        > would[color=green]
        >> recommend creating an executable and setting it up as a scheduled task[/color]
        > which[color=green]
        >> runs at a certain time, and then will shut itself down after being run
        >> for[/color]
        > a[color=green]
        >> certain amount of time.
        >>
        >> If you must use a service, then use the ServiceControll er class to
        >> get
        >> the service and shut it down.
        >>
        >> Hope this helps.
        >>
        >> --
        >> - Nicholas Paldino [.NET/C# MVP]
        >> - mvp@spam.guard. caspershouse.co m
        >>
        >>
        >> "SP" <EAI@sp.com> wrote in message
        >> news:eyLVy3g3EH A.536@TK2MSFTNG P10.phx.gbl...[color=darkred]
        >> > Hi All,
        >> >
        >> > I wrote a windows service which is supposed to stop after specified[/color][/color]
        > amount[color=green][color=darkred]
        >> > of time. I am calling OnStop() after specified time. OnStop() methods
        >> > executed but I dont see the service stopping.
        >> >
        >> > Please advise how to stop the service.
        >> >
        >> > Thanks,
        >> > SP
        >> >
        >> >[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • SP

          #5
          Re: Self terminating windows service

          Nicholas,

          No, I am not looking for shared access. I am using this windows service to
          read performance counters. The framework is utitlized for testing some
          applications and at that time I want to check the performance counters. I
          used a service for this task. Users have an option of starting and stopping
          the service with out using console applpication. Each time service is
          started it creates a new file with what ever performance counters user
          wishes to capture.

          So I guess there is either no easy way or no way we can have service stop
          itself.

          Thanks,
          Sasidhar.


          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          message news:uFWR0Oh3EH A.2572@tk2msftn gp13.phx.gbl...[color=blue]
          > SP,
          >
          > It sounds like you basically want shared access to a file from[/color]
          multiple[color=blue]
          > sources, right?
          >
          > A service IMO is massive overkill for this.
          >
          > Rather, you should use a Mutex in a class to block access to other
          > processes trying to write to the file.
          >
          > --
          > - Nicholas Paldino [.NET/C# MVP]
          > - mvp@spam.guard. caspershouse.co m
          >
          > "SP" <EAI@sp.com> wrote in message
          > news:OioVcIh3EH A.708@TK2MSFTNG P11.phx.gbl...[color=green]
          > > Hi Nicholas,
          > >
          > > Thank you for your reply. Let me give a brief background about what I am
          > > trying to achieve.
          > >
          > > As a part of framework I am working on, I have a console application and[/color][/color]
          a[color=blue][color=green]
          > > Windows service. Console application uses windows service. During
          > > execution
          > > console application calls windows service multiple times. I used
          > > ServiceControll er to start and stop. This part is running fine.
          > >
          > > The issue is, I made windows service a stand alone components which can[/color][/color]
          be[color=blue][color=green]
          > > executed from services console. Now the task of this service is to
          > > generate
          > > a csv file and keep on adding records to it till it is stopped. So just[/color][/color]
          to[color=blue][color=green]
          > > make sure that windows service does not keeps on adding records and
          > > exhaust
          > > all hard disk space because of human error, I wanted to limit the number
          > > of
          > > lines added by the service. Once the specified number of lines are[/color][/color]
          added,[color=blue][color=green]
          > > I
          > > want to terminate the service. As of now I am limiting the number of
          > > lines.
          > > But the service itself is not stopping.
          > >
          > > Please let me know whether there is anyway we can do that.
          > >
          > > Thanks,
          > >
          > > SP
          > >
          > > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
          > > in
          > > message news:uhf2$$g3EH A.3908@TK2MSFTN GP12.phx.gbl...[color=darkred]
          > >> SP,
          > >>
          > >> I don't think that this is a good idea, since the idea of a service
          > >> is
          > >> that it is supposed to be always running.
          > >>
          > >> If you have a need for a process to run at a certain time, then I[/color]
          > > would[color=darkred]
          > >> recommend creating an executable and setting it up as a scheduled task[/color]
          > > which[color=darkred]
          > >> runs at a certain time, and then will shut itself down after being run
          > >> for[/color]
          > > a[color=darkred]
          > >> certain amount of time.
          > >>
          > >> If you must use a service, then use the ServiceControll er class to
          > >> get
          > >> the service and shut it down.
          > >>
          > >> Hope this helps.
          > >>
          > >> --
          > >> - Nicholas Paldino [.NET/C# MVP]
          > >> - mvp@spam.guard. caspershouse.co m
          > >>
          > >>
          > >> "SP" <EAI@sp.com> wrote in message
          > >> news:eyLVy3g3EH A.536@TK2MSFTNG P10.phx.gbl...
          > >> > Hi All,
          > >> >
          > >> > I wrote a windows service which is supposed to stop after specified[/color]
          > > amount[color=darkred]
          > >> > of time. I am calling OnStop() after specified time. OnStop() methods
          > >> > executed but I dont see the service stopping.
          > >> >
          > >> > Please advise how to stop the service.
          > >> >
          > >> > Thanks,
          > >> > SP
          > >> >
          > >> >
          > >>
          > >>[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Nicholas Paldino [.NET/C# MVP]

            #6
            Re: Self terminating windows service

            SP,

            Once again, I have to say this is overkill. If you want to get
            performance counter information, why not just use the PerformanceCoun ter
            class in your console application, and have it write the file?

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

            "SP" <EAI@sp.com> wrote in message
            news:O0cbDWh3EH A.1408@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Nicholas,
            >
            > No, I am not looking for shared access. I am using this windows service to
            > read performance counters. The framework is utitlized for testing some
            > applications and at that time I want to check the performance counters. I
            > used a service for this task. Users have an option of starting and
            > stopping
            > the service with out using console applpication. Each time service is
            > started it creates a new file with what ever performance counters user
            > wishes to capture.
            >
            > So I guess there is either no easy way or no way we can have service stop
            > itself.
            >
            > Thanks,
            > Sasidhar.
            >
            >
            > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
            > in
            > message news:uFWR0Oh3EH A.2572@tk2msftn gp13.phx.gbl...[color=green]
            >> SP,
            >>
            >> It sounds like you basically want shared access to a file from[/color]
            > multiple[color=green]
            >> sources, right?
            >>
            >> A service IMO is massive overkill for this.
            >>
            >> Rather, you should use a Mutex in a class to block access to other
            >> processes trying to write to the file.
            >>
            >> --
            >> - Nicholas Paldino [.NET/C# MVP]
            >> - mvp@spam.guard. caspershouse.co m
            >>
            >> "SP" <EAI@sp.com> wrote in message
            >> news:OioVcIh3EH A.708@TK2MSFTNG P11.phx.gbl...[color=darkred]
            >> > Hi Nicholas,
            >> >
            >> > Thank you for your reply. Let me give a brief background about what I
            >> > am
            >> > trying to achieve.
            >> >
            >> > As a part of framework I am working on, I have a console application
            >> > and[/color][/color]
            > a[color=green][color=darkred]
            >> > Windows service. Console application uses windows service. During
            >> > execution
            >> > console application calls windows service multiple times. I used
            >> > ServiceControll er to start and stop. This part is running fine.
            >> >
            >> > The issue is, I made windows service a stand alone components which can[/color][/color]
            > be[color=green][color=darkred]
            >> > executed from services console. Now the task of this service is to
            >> > generate
            >> > a csv file and keep on adding records to it till it is stopped. So just[/color][/color]
            > to[color=green][color=darkred]
            >> > make sure that windows service does not keeps on adding records and
            >> > exhaust
            >> > all hard disk space because of human error, I wanted to limit the
            >> > number
            >> > of
            >> > lines added by the service. Once the specified number of lines are[/color][/color]
            > added,[color=green][color=darkred]
            >> > I
            >> > want to terminate the service. As of now I am limiting the number of
            >> > lines.
            >> > But the service itself is not stopping.
            >> >
            >> > Please let me know whether there is anyway we can do that.
            >> >
            >> > Thanks,
            >> >
            >> > SP
            >> >
            >> > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
            >> > wrote
            >> > in
            >> > message news:uhf2$$g3EH A.3908@TK2MSFTN GP12.phx.gbl...
            >> >> SP,
            >> >>
            >> >> I don't think that this is a good idea, since the idea of a
            >> >> service
            >> >> is
            >> >> that it is supposed to be always running.
            >> >>
            >> >> If you have a need for a process to run at a certain time, then I
            >> > would
            >> >> recommend creating an executable and setting it up as a scheduled task
            >> > which
            >> >> runs at a certain time, and then will shut itself down after being run
            >> >> for
            >> > a
            >> >> certain amount of time.
            >> >>
            >> >> If you must use a service, then use the ServiceControll er class to
            >> >> get
            >> >> the service and shut it down.
            >> >>
            >> >> Hope this helps.
            >> >>
            >> >> --
            >> >> - Nicholas Paldino [.NET/C# MVP]
            >> >> - mvp@spam.guard. caspershouse.co m
            >> >>
            >> >>
            >> >> "SP" <EAI@sp.com> wrote in message
            >> >> news:eyLVy3g3EH A.536@TK2MSFTNG P10.phx.gbl...
            >> >> > Hi All,
            >> >> >
            >> >> > I wrote a windows service which is supposed to stop after specified
            >> > amount
            >> >> > of time. I am calling OnStop() after specified time. OnStop()
            >> >> > methods
            >> >> > executed but I dont see the service stopping.
            >> >> >
            >> >> > Please advise how to stop the service.
            >> >> >
            >> >> > Thanks,
            >> >> > SP
            >> >> >
            >> >> >
            >> >>
            >> >>
            >> >
            >> >[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • SP

              #7
              Re: Self terminating windows service

              Nicholas,

              Thank you. I found this link online.



              SP


              "SP" <EAI@sp.com> wrote in message
              news:O0cbDWh3EH A.1408@TK2MSFTN GP10.phx.gbl...[color=blue]
              > Nicholas,
              >
              > No, I am not looking for shared access. I am using this windows service to
              > read performance counters. The framework is utitlized for testing some
              > applications and at that time I want to check the performance counters. I
              > used a service for this task. Users have an option of starting and[/color]
              stopping[color=blue]
              > the service with out using console applpication. Each time service is
              > started it creates a new file with what ever performance counters user
              > wishes to capture.
              >
              > So I guess there is either no easy way or no way we can have service stop
              > itself.
              >
              > Thanks,
              > Sasidhar.
              >
              >
              > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote[/color]
              in[color=blue]
              > message news:uFWR0Oh3EH A.2572@tk2msftn gp13.phx.gbl...[color=green]
              > > SP,
              > >
              > > It sounds like you basically want shared access to a file from[/color]
              > multiple[color=green]
              > > sources, right?
              > >
              > > A service IMO is massive overkill for this.
              > >
              > > Rather, you should use a Mutex in a class to block access to other
              > > processes trying to write to the file.
              > >
              > > --
              > > - Nicholas Paldino [.NET/C# MVP]
              > > - mvp@spam.guard. caspershouse.co m
              > >
              > > "SP" <EAI@sp.com> wrote in message
              > > news:OioVcIh3EH A.708@TK2MSFTNG P11.phx.gbl...[color=darkred]
              > > > Hi Nicholas,
              > > >
              > > > Thank you for your reply. Let me give a brief background about what I[/color][/color][/color]
              am[color=blue][color=green][color=darkred]
              > > > trying to achieve.
              > > >
              > > > As a part of framework I am working on, I have a console application[/color][/color][/color]
              and[color=blue]
              > a[color=green][color=darkred]
              > > > Windows service. Console application uses windows service. During
              > > > execution
              > > > console application calls windows service multiple times. I used
              > > > ServiceControll er to start and stop. This part is running fine.
              > > >
              > > > The issue is, I made windows service a stand alone components which[/color][/color][/color]
              can[color=blue]
              > be[color=green][color=darkred]
              > > > executed from services console. Now the task of this service is to
              > > > generate
              > > > a csv file and keep on adding records to it till it is stopped. So[/color][/color][/color]
              just[color=blue]
              > to[color=green][color=darkred]
              > > > make sure that windows service does not keeps on adding records and
              > > > exhaust
              > > > all hard disk space because of human error, I wanted to limit the[/color][/color][/color]
              number[color=blue][color=green][color=darkred]
              > > > of
              > > > lines added by the service. Once the specified number of lines are[/color][/color]
              > added,[color=green][color=darkred]
              > > > I
              > > > want to terminate the service. As of now I am limiting the number of
              > > > lines.
              > > > But the service itself is not stopping.
              > > >
              > > > Please let me know whether there is anyway we can do that.
              > > >
              > > > Thanks,
              > > >
              > > > SP
              > > >
              > > > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>[/color][/color][/color]
              wrote[color=blue][color=green][color=darkred]
              > > > in
              > > > message news:uhf2$$g3EH A.3908@TK2MSFTN GP12.phx.gbl...
              > > >> SP,
              > > >>
              > > >> I don't think that this is a good idea, since the idea of a[/color][/color][/color]
              service[color=blue][color=green][color=darkred]
              > > >> is
              > > >> that it is supposed to be always running.
              > > >>
              > > >> If you have a need for a process to run at a certain time, then I
              > > > would
              > > >> recommend creating an executable and setting it up as a scheduled[/color][/color][/color]
              task[color=blue][color=green][color=darkred]
              > > > which
              > > >> runs at a certain time, and then will shut itself down after being[/color][/color][/color]
              run[color=blue][color=green][color=darkred]
              > > >> for
              > > > a
              > > >> certain amount of time.
              > > >>
              > > >> If you must use a service, then use the ServiceControll er class[/color][/color][/color]
              to[color=blue][color=green][color=darkred]
              > > >> get
              > > >> the service and shut it down.
              > > >>
              > > >> Hope this helps.
              > > >>
              > > >> --
              > > >> - Nicholas Paldino [.NET/C# MVP]
              > > >> - mvp@spam.guard. caspershouse.co m
              > > >>
              > > >>
              > > >> "SP" <EAI@sp.com> wrote in message
              > > >> news:eyLVy3g3EH A.536@TK2MSFTNG P10.phx.gbl...
              > > >> > Hi All,
              > > >> >
              > > >> > I wrote a windows service which is supposed to stop after specified
              > > > amount
              > > >> > of time. I am calling OnStop() after specified time. OnStop()[/color][/color][/color]
              methods[color=blue][color=green][color=darkred]
              > > >> > executed but I dont see the service stopping.
              > > >> >
              > > >> > Please advise how to stop the service.
              > > >> >
              > > >> > Thanks,
              > > >> > SP
              > > >> >
              > > >> >
              > > >>
              > > >>
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • SP

                #8
                Re: Self terminating windows service

                Nicholas,

                I haven't done that much research before implementing this solution. But
                here are my observations.

                I implemented both a console app and service. Both achieve the same. While I
                see that windows service is using few of my system resources, console
                application is consuming more processing power. I dont want my performance
                counter reader to consume more resources. I dont know whether my argument
                makes that much sense or not but I am sure about my observation that console
                app is consuming more resources for this specific application..

                Thanks,
                SP



                "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                message news:O9ZUtYh3EH A.3388@TK2MSFTN GP15.phx.gbl...[color=blue]
                > SP,
                >
                > Once again, I have to say this is overkill. If you want to get
                > performance counter information, why not just use the PerformanceCoun ter
                > class in your console application, and have it write the file?
                >
                > --
                > - Nicholas Paldino [.NET/C# MVP]
                > - mvp@spam.guard. caspershouse.co m
                >
                > "SP" <EAI@sp.com> wrote in message
                > news:O0cbDWh3EH A.1408@TK2MSFTN GP10.phx.gbl...[color=green]
                > > Nicholas,
                > >
                > > No, I am not looking for shared access. I am using this windows service[/color][/color]
                to[color=blue][color=green]
                > > read performance counters. The framework is utitlized for testing some
                > > applications and at that time I want to check the performance counters.[/color][/color]
                I[color=blue][color=green]
                > > used a service for this task. Users have an option of starting and
                > > stopping
                > > the service with out using console applpication. Each time service is
                > > started it creates a new file with what ever performance counters user
                > > wishes to capture.
                > >
                > > So I guess there is either no easy way or no way we can have service[/color][/color]
                stop[color=blue][color=green]
                > > itself.
                > >
                > > Thanks,
                > > Sasidhar.
                > >
                > >
                > > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
                > > in
                > > message news:uFWR0Oh3EH A.2572@tk2msftn gp13.phx.gbl...[color=darkred]
                > >> SP,
                > >>
                > >> It sounds like you basically want shared access to a file from[/color]
                > > multiple[color=darkred]
                > >> sources, right?
                > >>
                > >> A service IMO is massive overkill for this.
                > >>
                > >> Rather, you should use a Mutex in a class to block access to other
                > >> processes trying to write to the file.
                > >>
                > >> --
                > >> - Nicholas Paldino [.NET/C# MVP]
                > >> - mvp@spam.guard. caspershouse.co m
                > >>
                > >> "SP" <EAI@sp.com> wrote in message
                > >> news:OioVcIh3EH A.708@TK2MSFTNG P11.phx.gbl...
                > >> > Hi Nicholas,
                > >> >
                > >> > Thank you for your reply. Let me give a brief background about what I
                > >> > am
                > >> > trying to achieve.
                > >> >
                > >> > As a part of framework I am working on, I have a console application
                > >> > and[/color]
                > > a[color=darkred]
                > >> > Windows service. Console application uses windows service. During
                > >> > execution
                > >> > console application calls windows service multiple times. I used
                > >> > ServiceControll er to start and stop. This part is running fine.
                > >> >
                > >> > The issue is, I made windows service a stand alone components which[/color][/color][/color]
                can[color=blue][color=green]
                > > be[color=darkred]
                > >> > executed from services console. Now the task of this service is to
                > >> > generate
                > >> > a csv file and keep on adding records to it till it is stopped. So[/color][/color][/color]
                just[color=blue][color=green]
                > > to[color=darkred]
                > >> > make sure that windows service does not keeps on adding records and
                > >> > exhaust
                > >> > all hard disk space because of human error, I wanted to limit the
                > >> > number
                > >> > of
                > >> > lines added by the service. Once the specified number of lines are[/color]
                > > added,[color=darkred]
                > >> > I
                > >> > want to terminate the service. As of now I am limiting the number of
                > >> > lines.
                > >> > But the service itself is not stopping.
                > >> >
                > >> > Please let me know whether there is anyway we can do that.
                > >> >
                > >> > Thanks,
                > >> >
                > >> > SP
                > >> >
                > >> > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
                > >> > wrote
                > >> > in
                > >> > message news:uhf2$$g3EH A.3908@TK2MSFTN GP12.phx.gbl...
                > >> >> SP,
                > >> >>
                > >> >> I don't think that this is a good idea, since the idea of a
                > >> >> service
                > >> >> is
                > >> >> that it is supposed to be always running.
                > >> >>
                > >> >> If you have a need for a process to run at a certain time, then[/color][/color][/color]
                I[color=blue][color=green][color=darkred]
                > >> > would
                > >> >> recommend creating an executable and setting it up as a scheduled[/color][/color][/color]
                task[color=blue][color=green][color=darkred]
                > >> > which
                > >> >> runs at a certain time, and then will shut itself down after being[/color][/color][/color]
                run[color=blue][color=green][color=darkred]
                > >> >> for
                > >> > a
                > >> >> certain amount of time.
                > >> >>
                > >> >> If you must use a service, then use the ServiceControll er class[/color][/color][/color]
                to[color=blue][color=green][color=darkred]
                > >> >> get
                > >> >> the service and shut it down.
                > >> >>
                > >> >> Hope this helps.
                > >> >>
                > >> >> --
                > >> >> - Nicholas Paldino [.NET/C# MVP]
                > >> >> - mvp@spam.guard. caspershouse.co m
                > >> >>
                > >> >>
                > >> >> "SP" <EAI@sp.com> wrote in message
                > >> >> news:eyLVy3g3EH A.536@TK2MSFTNG P10.phx.gbl...
                > >> >> > Hi All,
                > >> >> >
                > >> >> > I wrote a windows service which is supposed to stop after[/color][/color][/color]
                specified[color=blue][color=green][color=darkred]
                > >> > amount
                > >> >> > of time. I am calling OnStop() after specified time. OnStop()
                > >> >> > methods
                > >> >> > executed but I dont see the service stopping.
                > >> >> >
                > >> >> > Please advise how to stop the service.
                > >> >> >
                > >> >> > Thanks,
                > >> >> > SP
                > >> >> >
                > >> >> >
                > >> >>
                > >> >>
                > >> >
                > >> >
                > >>
                > >>[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: Self terminating windows service

                  SP,

                  Lets assume that reading the performance counters has a cost of X. Now
                  running a console app has a cost of Y, and running a service has a cost of
                  Z. When I say running a console app or service, I mean running just the
                  console, or the service, not the performance counter in the service.

                  So now, if you do it all in the console app, you have a cost of X + Y.
                  If you do it in the service, and have the console app activate the service,
                  you have a cost of X + Y + Z.

                  This is an oversimplificat ion, I admit.

                  In reality, reading the performance counters in a service versus a
                  console app are going to both take up the same amount of processing power,
                  since they are doing the same thing. Just because you take the code that is
                  consuming resources and putting it somewhere else doesn't mean that it isn't
                  paying the price for performing those operations.

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

                  "SP" <EAI@sp.com> wrote in message
                  news:%233Ciefh3 EHA.3000@TK2MSF TNGP15.phx.gbl. ..[color=blue]
                  > Nicholas,
                  >
                  > I haven't done that much research before implementing this solution. But
                  > here are my observations.
                  >
                  > I implemented both a console app and service. Both achieve the same. While
                  > I
                  > see that windows service is using few of my system resources, console
                  > application is consuming more processing power. I dont want my performance
                  > counter reader to consume more resources. I dont know whether my argument
                  > makes that much sense or not but I am sure about my observation that
                  > console
                  > app is consuming more resources for this specific application..
                  >
                  > Thanks,
                  > SP
                  >
                  >
                  >
                  > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
                  > in
                  > message news:O9ZUtYh3EH A.3388@TK2MSFTN GP15.phx.gbl...[color=green]
                  >> SP,
                  >>
                  >> Once again, I have to say this is overkill. If you want to get
                  >> performance counter information, why not just use the PerformanceCoun ter
                  >> class in your console application, and have it write the file?
                  >>
                  >> --
                  >> - Nicholas Paldino [.NET/C# MVP]
                  >> - mvp@spam.guard. caspershouse.co m
                  >>
                  >> "SP" <EAI@sp.com> wrote in message
                  >> news:O0cbDWh3EH A.1408@TK2MSFTN GP10.phx.gbl...[color=darkred]
                  >> > Nicholas,
                  >> >
                  >> > No, I am not looking for shared access. I am using this windows service[/color][/color]
                  > to[color=green][color=darkred]
                  >> > read performance counters. The framework is utitlized for testing some
                  >> > applications and at that time I want to check the performance counters.[/color][/color]
                  > I[color=green][color=darkred]
                  >> > used a service for this task. Users have an option of starting and
                  >> > stopping
                  >> > the service with out using console applpication. Each time service is
                  >> > started it creates a new file with what ever performance counters user
                  >> > wishes to capture.
                  >> >
                  >> > So I guess there is either no easy way or no way we can have service[/color][/color]
                  > stop[color=green][color=darkred]
                  >> > itself.
                  >> >
                  >> > Thanks,
                  >> > Sasidhar.
                  >> >
                  >> >
                  >> > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
                  >> > wrote
                  >> > in
                  >> > message news:uFWR0Oh3EH A.2572@tk2msftn gp13.phx.gbl...
                  >> >> SP,
                  >> >>
                  >> >> It sounds like you basically want shared access to a file from
                  >> > multiple
                  >> >> sources, right?
                  >> >>
                  >> >> A service IMO is massive overkill for this.
                  >> >>
                  >> >> Rather, you should use a Mutex in a class to block access to other
                  >> >> processes trying to write to the file.
                  >> >>
                  >> >> --
                  >> >> - Nicholas Paldino [.NET/C# MVP]
                  >> >> - mvp@spam.guard. caspershouse.co m
                  >> >>
                  >> >> "SP" <EAI@sp.com> wrote in message
                  >> >> news:OioVcIh3EH A.708@TK2MSFTNG P11.phx.gbl...
                  >> >> > Hi Nicholas,
                  >> >> >
                  >> >> > Thank you for your reply. Let me give a brief background about what
                  >> >> > I
                  >> >> > am
                  >> >> > trying to achieve.
                  >> >> >
                  >> >> > As a part of framework I am working on, I have a console application
                  >> >> > and
                  >> > a
                  >> >> > Windows service. Console application uses windows service. During
                  >> >> > execution
                  >> >> > console application calls windows service multiple times. I used
                  >> >> > ServiceControll er to start and stop. This part is running fine.
                  >> >> >
                  >> >> > The issue is, I made windows service a stand alone components which[/color][/color]
                  > can[color=green][color=darkred]
                  >> > be
                  >> >> > executed from services console. Now the task of this service is to
                  >> >> > generate
                  >> >> > a csv file and keep on adding records to it till it is stopped. So[/color][/color]
                  > just[color=green][color=darkred]
                  >> > to
                  >> >> > make sure that windows service does not keeps on adding records and
                  >> >> > exhaust
                  >> >> > all hard disk space because of human error, I wanted to limit the
                  >> >> > number
                  >> >> > of
                  >> >> > lines added by the service. Once the specified number of lines are
                  >> > added,
                  >> >> > I
                  >> >> > want to terminate the service. As of now I am limiting the number of
                  >> >> > lines.
                  >> >> > But the service itself is not stopping.
                  >> >> >
                  >> >> > Please let me know whether there is anyway we can do that.
                  >> >> >
                  >> >> > Thanks,
                  >> >> >
                  >> >> > SP
                  >> >> >
                  >> >> > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
                  >> >> > wrote
                  >> >> > in
                  >> >> > message news:uhf2$$g3EH A.3908@TK2MSFTN GP12.phx.gbl...
                  >> >> >> SP,
                  >> >> >>
                  >> >> >> I don't think that this is a good idea, since the idea of a
                  >> >> >> service
                  >> >> >> is
                  >> >> >> that it is supposed to be always running.
                  >> >> >>
                  >> >> >> If you have a need for a process to run at a certain time, then[/color][/color]
                  > I[color=green][color=darkred]
                  >> >> > would
                  >> >> >> recommend creating an executable and setting it up as a scheduled[/color][/color]
                  > task[color=green][color=darkred]
                  >> >> > which
                  >> >> >> runs at a certain time, and then will shut itself down after being[/color][/color]
                  > run[color=green][color=darkred]
                  >> >> >> for
                  >> >> > a
                  >> >> >> certain amount of time.
                  >> >> >>
                  >> >> >> If you must use a service, then use the ServiceControll er class[/color][/color]
                  > to[color=green][color=darkred]
                  >> >> >> get
                  >> >> >> the service and shut it down.
                  >> >> >>
                  >> >> >> Hope this helps.
                  >> >> >>
                  >> >> >> --
                  >> >> >> - Nicholas Paldino [.NET/C# MVP]
                  >> >> >> - mvp@spam.guard. caspershouse.co m
                  >> >> >>
                  >> >> >>
                  >> >> >> "SP" <EAI@sp.com> wrote in message
                  >> >> >> news:eyLVy3g3EH A.536@TK2MSFTNG P10.phx.gbl...
                  >> >> >> > Hi All,
                  >> >> >> >
                  >> >> >> > I wrote a windows service which is supposed to stop after[/color][/color]
                  > specified[color=green][color=darkred]
                  >> >> > amount
                  >> >> >> > of time. I am calling OnStop() after specified time. OnStop()
                  >> >> >> > methods
                  >> >> >> > executed but I dont see the service stopping.
                  >> >> >> >
                  >> >> >> > Please advise how to stop the service.
                  >> >> >> >
                  >> >> >> > Thanks,
                  >> >> >> > SP
                  >> >> >> >
                  >> >> >> >
                  >> >> >>
                  >> >> >>
                  >> >> >
                  >> >> >
                  >> >>
                  >> >>
                  >> >
                  >> >[/color]
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • SP

                    #10
                    Re: Self terminating windows service

                    I am not quite sure about how console applications and windows services are
                    handled by .NET framework. Putting everything into a console application has
                    few drawbacks. You will need to do thread processing because my console
                    application also does something other than starting and stopping the
                    service. The other thing is.. you cant control the program independently.
                    When I implemented thread processing my application was using lot of
                    resources. Using this approach my application never exceeded 2% CPU
                    utilization.

                    I used to believe in this
                    "Just because you take the code that is consuming resources and putting it
                    somewhere else doesn't mean that it isn't paying the price for performing
                    those operations."

                    But now I am not sure now... May be my application architecture was bad when
                    I had multi-thread solution.

                    My idea of separating is also driven by the thought of having multiple
                    independent services which can be reused. Influenced by SOA.

                    Thanks,
                    SP



                    "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                    message news:#Hk77lh3EH A.208@TK2MSFTNG P12.phx.gbl...[color=blue]
                    > SP,
                    >
                    > Lets assume that reading the performance counters has a cost of X.[/color]
                    Now[color=blue]
                    > running a console app has a cost of Y, and running a service has a cost of
                    > Z. When I say running a console app or service, I mean running just the
                    > console, or the service, not the performance counter in the service.
                    >
                    > So now, if you do it all in the console app, you have a cost of X + Y.
                    > If you do it in the service, and have the console app activate the[/color]
                    service,[color=blue]
                    > you have a cost of X + Y + Z.
                    >
                    > This is an oversimplificat ion, I admit.
                    >
                    > In reality, reading the performance counters in a service versus a
                    > console app are going to both take up the same amount of processing power,
                    > since they are doing the same thing. Just because you take the code that[/color]
                    is[color=blue]
                    > consuming resources and putting it somewhere else doesn't mean that it[/color]
                    isn't[color=blue]
                    > paying the price for performing those operations.
                    >
                    > --
                    > - Nicholas Paldino [.NET/C# MVP]
                    > - mvp@spam.guard. caspershouse.co m
                    >
                    > "SP" <EAI@sp.com> wrote in message
                    > news:%233Ciefh3 EHA.3000@TK2MSF TNGP15.phx.gbl. ..[color=green]
                    > > Nicholas,
                    > >
                    > > I haven't done that much research before implementing this solution. But
                    > > here are my observations.
                    > >
                    > > I implemented both a console app and service. Both achieve the same.[/color][/color]
                    While[color=blue][color=green]
                    > > I
                    > > see that windows service is using few of my system resources, console
                    > > application is consuming more processing power. I dont want my[/color][/color]
                    performance[color=blue][color=green]
                    > > counter reader to consume more resources. I dont know whether my[/color][/color]
                    argument[color=blue][color=green]
                    > > makes that much sense or not but I am sure about my observation that
                    > > console
                    > > app is consuming more resources for this specific application..
                    > >
                    > > Thanks,
                    > > SP
                    > >
                    > >
                    > >
                    > > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
                    > > in
                    > > message news:O9ZUtYh3EH A.3388@TK2MSFTN GP15.phx.gbl...[color=darkred]
                    > >> SP,
                    > >>
                    > >> Once again, I have to say this is overkill. If you want to get
                    > >> performance counter information, why not just use the[/color][/color][/color]
                    PerformanceCoun ter[color=blue][color=green][color=darkred]
                    > >> class in your console application, and have it write the file?
                    > >>
                    > >> --
                    > >> - Nicholas Paldino [.NET/C# MVP]
                    > >> - mvp@spam.guard. caspershouse.co m
                    > >>
                    > >> "SP" <EAI@sp.com> wrote in message
                    > >> news:O0cbDWh3EH A.1408@TK2MSFTN GP10.phx.gbl...
                    > >> > Nicholas,
                    > >> >
                    > >> > No, I am not looking for shared access. I am using this windows[/color][/color][/color]
                    service[color=blue][color=green]
                    > > to[color=darkred]
                    > >> > read performance counters. The framework is utitlized for testing[/color][/color][/color]
                    some[color=blue][color=green][color=darkred]
                    > >> > applications and at that time I want to check the performance[/color][/color][/color]
                    counters.[color=blue][color=green]
                    > > I[color=darkred]
                    > >> > used a service for this task. Users have an option of starting and
                    > >> > stopping
                    > >> > the service with out using console applpication. Each time service is
                    > >> > started it creates a new file with what ever performance counters[/color][/color][/color]
                    user[color=blue][color=green][color=darkred]
                    > >> > wishes to capture.
                    > >> >
                    > >> > So I guess there is either no easy way or no way we can have service[/color]
                    > > stop[color=darkred]
                    > >> > itself.
                    > >> >
                    > >> > Thanks,
                    > >> > Sasidhar.
                    > >> >
                    > >> >
                    > >> > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
                    > >> > wrote
                    > >> > in
                    > >> > message news:uFWR0Oh3EH A.2572@tk2msftn gp13.phx.gbl...
                    > >> >> SP,
                    > >> >>
                    > >> >> It sounds like you basically want shared access to a file from
                    > >> > multiple
                    > >> >> sources, right?
                    > >> >>
                    > >> >> A service IMO is massive overkill for this.
                    > >> >>
                    > >> >> Rather, you should use a Mutex in a class to block access to[/color][/color][/color]
                    other[color=blue][color=green][color=darkred]
                    > >> >> processes trying to write to the file.
                    > >> >>
                    > >> >> --
                    > >> >> - Nicholas Paldino [.NET/C# MVP]
                    > >> >> - mvp@spam.guard. caspershouse.co m
                    > >> >>
                    > >> >> "SP" <EAI@sp.com> wrote in message
                    > >> >> news:OioVcIh3EH A.708@TK2MSFTNG P11.phx.gbl...
                    > >> >> > Hi Nicholas,
                    > >> >> >
                    > >> >> > Thank you for your reply. Let me give a brief background about[/color][/color][/color]
                    what[color=blue][color=green][color=darkred]
                    > >> >> > I
                    > >> >> > am
                    > >> >> > trying to achieve.
                    > >> >> >
                    > >> >> > As a part of framework I am working on, I have a console[/color][/color][/color]
                    application[color=blue][color=green][color=darkred]
                    > >> >> > and
                    > >> > a
                    > >> >> > Windows service. Console application uses windows service. During
                    > >> >> > execution
                    > >> >> > console application calls windows service multiple times. I used
                    > >> >> > ServiceControll er to start and stop. This part is running fine.
                    > >> >> >
                    > >> >> > The issue is, I made windows service a stand alone components[/color][/color][/color]
                    which[color=blue][color=green]
                    > > can[color=darkred]
                    > >> > be
                    > >> >> > executed from services console. Now the task of this service is to
                    > >> >> > generate
                    > >> >> > a csv file and keep on adding records to it till it is stopped. So[/color]
                    > > just[color=darkred]
                    > >> > to
                    > >> >> > make sure that windows service does not keeps on adding records[/color][/color][/color]
                    and[color=blue][color=green][color=darkred]
                    > >> >> > exhaust
                    > >> >> > all hard disk space because of human error, I wanted to limit the
                    > >> >> > number
                    > >> >> > of
                    > >> >> > lines added by the service. Once the specified number of lines are
                    > >> > added,
                    > >> >> > I
                    > >> >> > want to terminate the service. As of now I am limiting the number[/color][/color][/color]
                    of[color=blue][color=green][color=darkred]
                    > >> >> > lines.
                    > >> >> > But the service itself is not stopping.
                    > >> >> >
                    > >> >> > Please let me know whether there is anyway we can do that.
                    > >> >> >
                    > >> >> > Thanks,
                    > >> >> >
                    > >> >> > SP
                    > >> >> >
                    > >> >> > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
                    > >> >> > wrote
                    > >> >> > in
                    > >> >> > message news:uhf2$$g3EH A.3908@TK2MSFTN GP12.phx.gbl...
                    > >> >> >> SP,
                    > >> >> >>
                    > >> >> >> I don't think that this is a good idea, since the idea of a
                    > >> >> >> service
                    > >> >> >> is
                    > >> >> >> that it is supposed to be always running.
                    > >> >> >>
                    > >> >> >> If you have a need for a process to run at a certain time,[/color][/color][/color]
                    then[color=blue][color=green]
                    > > I[color=darkred]
                    > >> >> > would
                    > >> >> >> recommend creating an executable and setting it up as a scheduled[/color]
                    > > task[color=darkred]
                    > >> >> > which
                    > >> >> >> runs at a certain time, and then will shut itself down after[/color][/color][/color]
                    being[color=blue][color=green]
                    > > run[color=darkred]
                    > >> >> >> for
                    > >> >> > a
                    > >> >> >> certain amount of time.
                    > >> >> >>
                    > >> >> >> If you must use a service, then use the ServiceControll er[/color][/color][/color]
                    class[color=blue][color=green]
                    > > to[color=darkred]
                    > >> >> >> get
                    > >> >> >> the service and shut it down.
                    > >> >> >>
                    > >> >> >> Hope this helps.
                    > >> >> >>
                    > >> >> >> --
                    > >> >> >> - Nicholas Paldino [.NET/C# MVP]
                    > >> >> >> - mvp@spam.guard. caspershouse.co m
                    > >> >> >>
                    > >> >> >>
                    > >> >> >> "SP" <EAI@sp.com> wrote in message
                    > >> >> >> news:eyLVy3g3EH A.536@TK2MSFTNG P10.phx.gbl...
                    > >> >> >> > Hi All,
                    > >> >> >> >
                    > >> >> >> > I wrote a windows service which is supposed to stop after[/color]
                    > > specified[color=darkred]
                    > >> >> > amount
                    > >> >> >> > of time. I am calling OnStop() after specified time. OnStop()
                    > >> >> >> > methods
                    > >> >> >> > executed but I dont see the service stopping.
                    > >> >> >> >
                    > >> >> >> > Please advise how to stop the service.
                    > >> >> >> >
                    > >> >> >> > Thanks,
                    > >> >> >> > SP
                    > >> >> >> >
                    > >> >> >> >
                    > >> >> >>
                    > >> >> >>
                    > >> >> >
                    > >> >> >
                    > >> >>
                    > >> >>
                    > >> >
                    > >> >
                    > >>
                    > >>[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    Working...