3

私のWindowsサービスでは、ThreadPool(バックグラウンドであることを意味します)を使用して「子」スレッドを生成し、タスクを実行する「親」フォアグラウンドスレッドを1つ作成します。

Windowsサービスの停止時にフォアグラウンドスレッドを適切に閉じる最良の方法は何ですか?

これが私の現在の実装です(タスク固有のロジックから取り除かれています):

public partial class TaskScheduler : ServiceBase
{
   private static AutoResetEvent _finishedTaskAutoResetEvent = new AutoResetEvent(false);

   //This flag is used to increase chances of the Spawning Thread to finish gracefully when service stops.
   private bool StopRequested { get; set; }

   private int _executingTasksCount;

   private int ExecutingTasksCount { get { return _executingTasksCount; } }

   private void IncCurrentTasksCount()
   {
       Interlocked.Increment(ref _executingTasksCount);
   }

   private void DecCurrentTasksCount()
   {
       Interlocked.Decrement(ref _executingTasksCount);
   }

   public TaskScheduler()
   {
       InitializeComponent();

       Thread spawningThread = new Thread(DoSpawnTaskExecutionThreads);

       spawningThread.Name = "Spawning Thread";
       spawningThread.IsBackground = false;
       spawningThread.Start();
   }

   protected override void OnStart(string[] args)
   {
   }

   protected override void OnStop()
   {
       StopRequested = true;
   }

   private void DoSpawnTaskExecutionThreads()
   {
       //We check StopRequested to try and finish this thread gracefully when service stops.
       while (!StopRequested)
       {
           while (!StopRequested && ExecutingTasksCount < MaxPooledTasks)
           {
               ThreadPool.QueueUserWorkItem(ExecuteTask, new Task());

               IncCurrentTasksCount();
           }

           _finishedTaskAutoResetEvent.WaitOne();
       }

       //Either all task execution threads will finish or the process will be terminated forcibly.
       while (ExecutingTasksCount > 0)
       {
           Thread.Sleep(200); //Check five times a second.
       }

       _eventLog.WriteEntry("The Spawning Thread finished along with task execution threads.");
   }

   private void ExecuteTask(object state)
   {
       try
       {
           Task task = (Task)state;

           task.Execute();
       }
       catch
       {
           // Handle exception.
       }
       finally
       {
           DecCurrentTasksCount();
           _finishedTaskAutoResetEvent.Set();
       }
   }

}

4

3 に答える 3

3

コードにいくつか問題があります。

  • のチェックStopRequestedはスレッドセーフではありません。
  • のチェックExecutingTaskCountはスレッドセーフではありません。
  • カウントを維持しないため_finishedTaskAutoResetEventAutoResetEvent信号が失われる可能性があるためです。それがあなたの望みかもしれませんが、入れ子になったループWaitHandleが奇妙な回転をする可能性があります。while

コードをリファクタリングする方法は次のとおりです。CountdownEvent.NET 4.0 で利用可能なクラスを使用します。

public class TaskScheduler : ServiceBase
{
    private m_Stop as ManualResetEvent = new ManualResetEvent(false);

    protected override void OnStart(string[] args)           
    {           
      var thread = new Thread(DoSpawnTaskExecutionThreads);
      thread.Name = "Spawning Thread";
      thread.IsBackground = false;
      thread.Start();
    }           

    protected override OnStop()
    {
      m_Stop.Set();
    }

    public DoSpawnTaskExecutionThreads()
    {
      // The semaphore will control how many concurrent tasks can run.
      var pool = new Semaphore(MaxPooledThreads, MaxPooledThreads);

      // The countdown event will be used to wait for any pending tasks.
      // Initialize the count to 1 so that we treat this thread as if it 
      // were a work item. This is necessary to avoid a subtle race
      // with a real work item that completes quickly.
      var tasks = new CountdownEvent(1);

      // This array will be used to control the spinning of the loop.
      var all = new WaitHandle[] { pool, m_Stop };

      while (WaitHandle.WaitAny(all) == 0)
      {
        // Indicate that there is another task.
        tasks.AddCount();

        // Queue the task.
        Thread.QueueUserWorkItem(
          (state) =>
          {
            try
            {
              var task = (Task)state;
              task.Execute();
            }
            finally
            {
              pool.Release(); // Allow another task to be queued.
              tasks.Signal(); // Indicate that this task is complete.
            }
          }, new Task());
      }

      // Indicate that the main thread is complete.
      tasks.Signal();

      // Wait for all pending tasks.
      tasks.Wait();
    }
}
于 2010-10-25T16:54:17.923 に答える
2

ここに 1 つの問題があります。

StopRequested は自動プロパティであってはなりません。volatileとマークするには、バッキング フィールドを持つプロパティとしてこれを定義する必要があります。

private volatile bool stopRequested;
private bool StopRequested
{
    get { return this.stopRequested; }
    set { this.stopRequested = value; }
}

これがないと、サービスによって設定されたときに、終了条件がスレッドによって (少なくともすぐには) 認識されない可能性があります。

また、.NET 4 がオプションの場合、 と を使用して実行できる、より単純な設計がCancellationTokenありBlockingCollection<T>ます。

于 2010-10-25T15:51:13.780 に答える
0

Join メソッドを使用して、スレッドを「適切に」強制終了できます。MSDNには、メソッドに関する情報がいくつかあります。

于 2010-10-25T15:52:20.310 に答える