Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Stopping a Windows Service in a Timely Manner...

Stopping a Windows Service in a Timely Manner...

Scheduled Pinned Locked Moved C#
csharpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    agent00zelda
    wrote on last edited by
    #1

    Hello! I recently started writing my first Windows Service project in C#, and I'm at a point now where I have a lot of time to perfect things before I continue. I was wondering what's considered generally "good practice" with regards to Windows Services. Specifically, the stopping mechanism. When my service starts, it kicks off a few threads that perform some file manipulation. When the service stops, I want it to shut those threads down gracefully. Ideally, shutting down the service puts the file system in a state where the manipulation can be easily resumed upon restarting the service. So, when shutting down the service, I'd prefer to let one of the threads (if it's currently doing work) finish the works it's already started, but in some cases, this can take several minutes. Is this considered bad practice, or is this something I shouldn't really be concerned with? Are there other things anyone thinks I should consider?

    P 1 Reply Last reply
    0
    • A agent00zelda

      Hello! I recently started writing my first Windows Service project in C#, and I'm at a point now where I have a lot of time to perfect things before I continue. I was wondering what's considered generally "good practice" with regards to Windows Services. Specifically, the stopping mechanism. When my service starts, it kicks off a few threads that perform some file manipulation. When the service stops, I want it to shut those threads down gracefully. Ideally, shutting down the service puts the file system in a state where the manipulation can be easily resumed upon restarting the service. So, when shutting down the service, I'd prefer to let one of the threads (if it's currently doing work) finish the works it's already started, but in some cases, this can take several minutes. Is this considered bad practice, or is this something I shouldn't really be concerned with? Are there other things anyone thinks I should consider?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      It sounds OK to me, though "several minutes" sounds excessive. Is there a way to have them stop after the current operation even though there are still other things to do?

      agent00zelda wrote:

      it kicks off a few threads that perform some file manipulation

      Can those be in their own Services?

      A 1 Reply Last reply
      0
      • P PIEBALDconsult

        It sounds OK to me, though "several minutes" sounds excessive. Is there a way to have them stop after the current operation even though there are still other things to do?

        agent00zelda wrote:

        it kicks off a few threads that perform some file manipulation

        Can those be in their own Services?

        A Offline
        A Offline
        agent00zelda
        wrote on last edited by
        #3

        Thank you for your response!!

        PIEBALDconsult wrote:

        Can those be in their own Services?

        Actually, yes, and they are.. I guess I should have mentioned that part :) The way I had it implemented before I made the initial post was I pretty much let it finish everything until it was able to submit the data to the other services. But Windows spazzed out because it couldn't stop the main service until it finished the preceding operations. What I ended up doing was defining like a project "state" DataTable that could be exported and imported again when the service restarts. Your suggestion was well-taken -- pretty much made the current operation finish and then updated the project state based on that. I don't know if using a DataTable is the best approach, but it seems to work much better, since it takes considerably less time to stop the service. Still open to suggestions if you or anyone can think of a better approach, though!! I guess I'm in a C# experimentation phase :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups