开发者

Start and stop(forced) a threaded job

I want to know a proper way to start and stop a threaded job forced and unforced. Is this the proper way to stop a Thread?

    public class ProcessDataJob : IJob
    {
        private ConcurrentQueue<byte[]> _dataQueue = new ConcurrentQueue<byte[]>();
        private volatile bool _stop = false;
        private volatile bool _forceStop = false;
        private Thr开发者_运维问答ead _thread;
        private int _timeOut = 1000;

        public void Start()
        {
            _stop = false;
            _forceStop = false;

            _thread = new Thread(ProcessData);
            _thread.Start();
        }

        private void ProcessData()
        {
            while (!_stop || _dataQueue.Count > 0)
            {
                if(_forceStop) return;

                byte[] data;
                if(_dataQueue.TryDequeue(data))
                {
                   //Process data
                   //.....//
                }
            }
        }

        public void Stop(bool force)
        {
            _stop = true;
            _forceStop = force;
            _thread.Join(_timeOut); 
        }

        public void Enqueue(byte[] data)
        {
            _dataQueue.Enqueue(data);
        }
    }


There is no proper way to forcibly kill a thread.

There are several ways to do it, but none of them are proper.

Forcibly killing a thread is what you should do only if you need to terminate the program, or unload the appdomain containing the thread, and don´t care about any data structures left dangling in a corrupted/bad/locked state, because they will be gone in a short while as well.

There´s plenty of advice on the internet about how bad/evil Thread.Abort is, so don´t do it.

Instead, write proper cooperative threading. The thread(s) should themselves check a flag (event, volatile bool field, etc.) and then voluntairly exit when nicely asked to do so.

That is the proper way.


This is the way that I've done it in the past, with one difference. I've had threads unexpectedly hang in the past, which means that that loop of yours doesn't come back with an answer. To address this I have all thread using classes, like yours, register with a 'manager' class who is responsible for participating in key things like Forced Stop. The threaded class has a reference to the manager, and when a forced stop is done it calls a method on the manager which effectively is a timer. If by the time the timer has gone off the threaded class hasn't set a state flag to STOPPED then the manager calls abort on it.

The key thing for me was not just the calling 'stop' but the confirmation that 'stop' had occurred, and understanding that it will be a non-deterministic amount of time but that 'after a reasonable amount of time' that I should give up and move on.


You could use the .NET ThreadPool class, this way you don't have to handle the stack yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜