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. Windows Service with threading issues

Windows Service with threading issues

Scheduled Pinned Locked Moved C#
databasehelpquestionworkspace
2 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.
  • J Offline
    J Offline
    Joshua Lunsford
    wrote on last edited by
    #1

    I have an issue with stopping threads in a windows service application I was wondering if anyone can help. let me set it up for you. In a windows service you have to use the OnStart method to initialize your application... but you cannot run any loops or sequence of events that contain loops or the state of the application will be in an "starting" service mode indefinitely. So I start a thread call Maint to handle the application. The application has threee main functions at the moment that interface with WAN file shares and a sql db. I run these parallel each in there own thread(and they dont interact with eachother). so in the OnStart method: maint Maint = new maint(); maintThread = new Thread(new ThreadStart(Maint.start)); maintThread.Start(); in that start method i call: importassets ia = new importassets(); importThread = new Thread(new ThreadStart(ia.importasset)); importThread.Start(); updatereg ur = new updatereg(); updateThread = new Thread(new ThreadStart(ur.runupdate)); updateThread.Start(); forcelogins fl = new forcelogins(); loginThread = new Thread(new ThreadStart(fl.runlogins)); loginThread.Start(); //This section forces the importThread to restart hourly int startTime = System.Environment.TickCount; int runTime = startTime+3600000; while(true) { int nowTime = System.Environment.TickCount; FAISservice fs = new FAISservice(); if(!fs.isRunning) break; if(runTime < nowTime) { importThread.Abort(); Thread.Sleep(3000); startImport(); runTime = nowTime+3600000; } Thread.Sleep(10000); } Now, all works fine until the OnStop() method gets called by the service being stopped or restarted. in the OnStop() method i run: isRunning = false; maintThread.Abort(); maint Maint = new maint(); Maint.stop(); and in the stop() method i call: importThread.Abort(); updateThread.Abort(); loginThread.Abort(); Now I'm locked in a "STOPPING" state and i have to kill the process... any clues? also... my above 3 threads start while(true) loops that only BREAK when isRunning=false.

    T 1 Reply Last reply
    0
    • J Joshua Lunsford

      I have an issue with stopping threads in a windows service application I was wondering if anyone can help. let me set it up for you. In a windows service you have to use the OnStart method to initialize your application... but you cannot run any loops or sequence of events that contain loops or the state of the application will be in an "starting" service mode indefinitely. So I start a thread call Maint to handle the application. The application has threee main functions at the moment that interface with WAN file shares and a sql db. I run these parallel each in there own thread(and they dont interact with eachother). so in the OnStart method: maint Maint = new maint(); maintThread = new Thread(new ThreadStart(Maint.start)); maintThread.Start(); in that start method i call: importassets ia = new importassets(); importThread = new Thread(new ThreadStart(ia.importasset)); importThread.Start(); updatereg ur = new updatereg(); updateThread = new Thread(new ThreadStart(ur.runupdate)); updateThread.Start(); forcelogins fl = new forcelogins(); loginThread = new Thread(new ThreadStart(fl.runlogins)); loginThread.Start(); //This section forces the importThread to restart hourly int startTime = System.Environment.TickCount; int runTime = startTime+3600000; while(true) { int nowTime = System.Environment.TickCount; FAISservice fs = new FAISservice(); if(!fs.isRunning) break; if(runTime < nowTime) { importThread.Abort(); Thread.Sleep(3000); startImport(); runTime = nowTime+3600000; } Thread.Sleep(10000); } Now, all works fine until the OnStop() method gets called by the service being stopped or restarted. in the OnStop() method i run: isRunning = false; maintThread.Abort(); maint Maint = new maint(); Maint.stop(); and in the stop() method i call: importThread.Abort(); updateThread.Abort(); loginThread.Abort(); Now I'm locked in a "STOPPING" state and i have to kill the process... any clues? also... my above 3 threads start while(true) loops that only BREAK when isRunning=false.

      T Offline
      T Offline
      Tom Delany
      wrote on last edited by
      #2

      We have a similar situation, and basically, in our thread classes, we have created a Shutdown() method. The threads themselves this method at different points in their loops (where it is practical for them to gracefully stop), and simply shut themselves down when this method returns true. The threads do not stop immediately, like from a Thread.Abort(), but this way they are given a chance to terminate gracefully. In the thread proc, the method is coded as so: public class SomeThread { // This variable is used by multiple threads, so it must be thread-safe. // Do not access it directly -- use the ShutDown property instead. private bool m_bTimeToShutdown = false; private Object ShutDownLock = new Object(); // Other variables and methods go here...... /// /// This property can be set to tell the main loop to stop processing. /// The main loop continues to run as long as this property is false. /// public bool ShutDown { get { bool rslt; lock (ShutDownLock) { rslt = m_bTimeToShutdown; } return rslt; } set { lock (ShutDownLock) { m_bTimeToShutdown = value; } } } } Calling example (in the Main service): /// /// Stop this service. /// protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. log.Info("Shutting down some thread"); // This is just a sample logging call. SomeThreadThrdProc.ShutDown = true; } I hope this helps you. Tom Delany

      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