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. Design question about windows services...

Design question about windows services...

Scheduled Pinned Locked Moved C#
questiondesign
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.
  • C Offline
    C Offline
    CherezZaboro
    wrote on last edited by
    #1

    So in OnStop I create and start a thread with new Thread(new ThreadStart(funk)), right? Thread is doing something and then Stop command comes along and naturaly I get OnStop called. Everything is OK so far. I post to my thread a message somehow for it to quit and OnStop returns right away. SCM thinks that everything was ok. But my worker thread has some work to finish before quitting and it may take time. So it continues until it's done BUT magically the service process disappears in about 20 sec after OnStop returned cutting the execution of my thread right in the middle (or so it seems) of a routine. X| How do I avoid that? :confused: I want that thread to finish normaly.

    B 1 Reply Last reply
    0
    • C CherezZaboro

      So in OnStop I create and start a thread with new Thread(new ThreadStart(funk)), right? Thread is doing something and then Stop command comes along and naturaly I get OnStop called. Everything is OK so far. I post to my thread a message somehow for it to quit and OnStop returns right away. SCM thinks that everything was ok. But my worker thread has some work to finish before quitting and it may take time. So it continues until it's done BUT magically the service process disappears in about 20 sec after OnStop returned cutting the execution of my thread right in the middle (or so it seems) of a routine. X| How do I avoid that? :confused: I want that thread to finish normaly.

      B Offline
      B Offline
      Burt Harris
      wrote on last edited by
      #2

      You need to wait in your OnStop function until your service has finished shutting down. You might do this with a ManualResetEvent like this: ManualResetEvent done = new ManualResetEvent(false); protected override void OnStop() { // Tell your service to shut down done.WaitOne(); }
      Then in your other routine, running on the other thread, just call done.Set(); when you are all done, just before returning. Burt Harris

      C 1 Reply Last reply
      0
      • B Burt Harris

        You need to wait in your OnStop function until your service has finished shutting down. You might do this with a ManualResetEvent like this: ManualResetEvent done = new ManualResetEvent(false); protected override void OnStop() { // Tell your service to shut down done.WaitOne(); }
        Then in your other routine, running on the other thread, just call done.Set(); when you are all done, just before returning. Burt Harris

        C Offline
        C Offline
        CherezZaboro
        wrote on last edited by
        #3

        But I already tried even a message box in onstop method. That should've keep it alive for as long as there is the message box, right? Wrong! The process magicaly dissapears in 20 seconds after stop button was hit in SCM and it seem it doesn't metter what I do. The execution of a function just gets cut off right in the middle of a routine. Looks like SCM uses "terminate process" or something...

        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