[C#]在Windows Service中使用ThreadPool

用C#实现Windows Service的时候,如果该服务会起多个线程来执行一些后台程序,那么,使用.NET自带的ThreadPool将会是一个异常糟糕的体验。

问题描述:

我有一个Windows Service,其主要任务是监视一个数据库,当数据库只有新的Task产生的时候,将该Task的状态改为Running并且读取其中的信息,建立一个新的线程去运行。

当该服务同时只运行一个线程的时候,一切都很美好,但是如果我们想要设定最多n个线程并行执行的时候,自然而然地,我们会想到用.Net中的ThreadPool类:

ThreadPool.QueueTask(WorkingThread, arg);

这种方式的好处是实现非常简单,而且ThreadPool本身的实现是经过M$测试的,所以可以比较安心地使用。

 

但是糟糕的问题出现了。当ThreadPool中有线程正在运行时,你会发现该服务在关闭时,OnStop()函数会迟迟不肯执行,导致服务不能正常关闭。网上搜索了一下,发现:

http://stackoverflow.com/questions/10643217/windows-service-onstop-not-called

大意就是如果ThreadPool中有线程正在运行的话,OnStop有时候就是会不执行。在这里我没有想出会阻止OnStop执行的原因,猜想和Windows Service本身的机制有关。另一方面,在使用ThreadPool的时候,我们是不能打断正在运行的线程的,你也可以发现,ThreadPool是没有Cancel或者Abort之类的Method的:

http://stackoverflow.com/questions/11986285/cancelling-a-threadpool-workitem-with-thread-interrupt

所以要使线程池中的线程停止的唯一办法,是让他们读取一个程序是否已经中止的flag,当我们需要线程池停止的时候,去设置这个flag,让线程内部的逻辑自己停止。

        private void WorkingThread(Object obj)
        {   
            while(serviceRunning){
              //do something
       }
     }

 

但是,即使你使用了如上的方法,ThreadPool导致Windows Service不能终止的问题依然存在。

解决方案很惊艳:

http://stackoverflow.com/questions/435668/code-for-a-simple-thread-pool-in-c-sharp

using System;
using System.Collections.Generic;
using System.Threading;

namespace SimpleThreadPool
{
    public sealed class Pool : IDisposable
    {
        public Pool(int size)
        {
            this._workers = new LinkedList<Thread>();
            for (var i = 0; i < size; ++i)
            {
                var worker = new Thread(this.Worker) { Name = string.Concat("Worker ", i) };
                worker.Start();
                this._workers.AddLast(worker);
            }
        }

        public void Dispose()
        {
            var waitForThreads = false;
            lock (this._tasks)
            {
                if (!this._disposed)
                {
                    GC.SuppressFinalize(this);

                    this._disallowAdd = true; // wait for all tasks to finish processing while not allowing any more new tasks
                    while (this._tasks.Count > 0)
                    {
                        Monitor.Wait(this._tasks);
                    }

                    this._disposed = true;
                    Monitor.PulseAll(this._tasks); // wake all workers (none of them will be active at this point; disposed flag will cause then to finish so that we can join them)
                    waitForThreads = true;
                }
            }
            if (waitForThreads)
            {
                foreach (var worker in this._workers)
                {
                    worker.Join();
                }
            }
        }

        public void QueueTask(Action task)
        {
            lock (this._tasks)
            {
                if (this._disallowAdd) { throw new InvalidOperationException("This Pool instance is in the process of being disposed, can't add anymore"); }
                if (this._disposed) { throw new ObjectDisposedException("This Pool instance has already been disposed"); }
                this._tasks.AddLast(task);
                Monitor.PulseAll(this._tasks); // pulse because tasks count changed
            }
        }

        private void Worker()
        {
            Action task = null;
            while (true) // loop until threadpool is disposed
            {
                lock (this._tasks) // finding a task needs to be atomic
                {
                    while (true) // wait for our turn in _workers queue and an available task
                    {
                        if (this._disposed)
                        {
                            return;
                        }
                        if (null != this._workers.First && object.ReferenceEquals(Thread.CurrentThread, this._workers.First.Value) && this._tasks.Count > 0) // we can only claim a task if its our turn (this worker thread is the first entry in _worker queue) and there is a task available
                        {
                            task = this._tasks.First.Value;
                            this._tasks.RemoveFirst();
                            this._workers.RemoveFirst();
                            Monitor.PulseAll(this._tasks); // pulse because current (First) worker changed (so that next available sleeping worker will pick up its task)
                            break; // we found a task to process, break out from the above 'while (true)' loop
                        }
                        Monitor.Wait(this._tasks); // go to sleep, either not our turn or no task to process
                    }
                }

                task(); // process the found task
                this._workers.AddLast(Thread.CurrentThread);
                task = null;
            }
        }

        private readonly LinkedList<Thread> _workers; // queue of worker threads ready to process actions
        private readonly LinkedList<Action> _tasks = new LinkedList<Action>(); // actions to be processed by worker threads
        private bool _disallowAdd; // set to true when disposing queue but there are still tasks pending
        private bool _disposed; // set to true when disposing queue and no more tasks are pending
    }


    public static class Program
    {
        static void Main()
        {
            using (var pool = new Pool(5))
            {
                var random = new Random();
                Action<int> randomizer = (index =>
                {
                    Console.WriteLine("{0}: Working on index {1}", Thread.CurrentThread.Name, index);
                    Thread.Sleep(random.Next(20, 400));
                    Console.WriteLine("{0}: Ending {1}", Thread.CurrentThread.Name, index);
                });

                for (var i = 0; i < 40; ++i)
                {
                    var i1 = i;
                    pool.QueueTask(() => randomizer(i1));
                }
            }
        }
    }
}

我稍稍修改了一下那个Pool,改成和ThreadPool一样的接口:

    public sealed class Pool : IDisposable
    {
        public Pool(int size)
        {
            this._workers = new LinkedList<Thread>();
            for (var i = 0; i < size; ++i)
            {
                var worker = new Thread(this.Worker) { Name = string.Concat("Worker ", i) };
                worker.Start();
                this._workers.AddLast(worker);
            }
        }

        public void Dispose()
        {
            var waitForThreads = false;
            lock (this._tasks)
            {
                if (!this._disposed)
                {
                    GC.SuppressFinalize(this);

                    this._disallowAdd = true; // wait for all tasks to finish processing while not allowing any more new tasks
                    while (this._tasks.Count > 0)
                    {
                        Monitor.Wait(this._tasks);
                    }

                    this._disposed = true;
                    Monitor.PulseAll(this._tasks); // wake all workers (none of them will be active at this point; disposed flag will cause then to finish so that we can join them)
                    waitForThreads = true;
                }
            }
            if (waitForThreads)
            {
                foreach (var worker in this._workers)
                {
                    worker.Join();
                }
            }
        }

        public void QueueTask(WaitCallback task, object arg)
        {
            lock (this._tasks)
            {
                if (this._disallowAdd) { throw new InvalidOperationException("This Pool instance is in the process of being disposed, can't add anymore"); }
                if (this._disposed) { throw new ObjectDisposedException("This Pool instance has already been disposed"); }
                this._tasks.AddLast(task);
                this._args.AddLast(arg);
                Monitor.PulseAll(this._tasks); // pulse because tasks count changed
            }
        }

        private void Worker()
        {
            WaitCallback task = null;
            object arg = null;
            while (true) // loop until threadpool is disposed
            {
                lock (this._tasks) // finding a task needs to be atomic
                {
                    while (true) // wait for our turn in _workers queue and an available task
                    {
                        if (this._disposed)
                        {
                            return;
                        }
                        if (null != this._workers.First && object.ReferenceEquals(Thread.CurrentThread, this._workers.First.Value) && this._tasks.Count > 0) // we can only claim a task if its our turn (this worker thread is the first entry in _worker queue) and there is a task available
                        {
                            task = this._tasks.First.Value;
                            arg = this._args.First.Value;
                            this._tasks.RemoveFirst();
                            this._args.RemoveFirst();
                            this._workers.RemoveFirst();
                            Monitor.PulseAll(this._tasks); // pulse because current (First) worker changed (so that next available sleeping worker will pick up its task)
                            break; // we found a task to process, break out from the above 'while (true)' loop
                        }
                        Monitor.Wait(this._tasks); // go to sleep, either not our turn or no task to process
                    }
                }

                task(arg); // process the found task
                this._workers.AddLast(Thread.CurrentThread);
                task = null;
                arg = null;
            }
        }

        private readonly LinkedList<Thread> _workers; // queue of worker threads ready to process WaitCallbacks
        private readonly LinkedList<WaitCallback> _tasks = new LinkedList<WaitCallback>(); // WaitCallbacks to be processed by worker threads
        private readonly LinkedList<Object> _args = new LinkedList<object>(); // Arguments to be processed by worker threads
        private bool _disallowAdd; // set to true when disposing queue but there are still tasks pending
        private bool _disposed; // set to true when disposing queue and no more tasks are pending
    }

这样就可以使用带参数的Thread了。经过测试,这个被Post在Stackoverflow Answer里面的小程序非常靠谱。

 

 

posted @ 2013-05-09 00:31  magicdlf  阅读(825)  评论(0编辑  收藏  举报