Domanda

I have a windows service which will start and stop the execution of some process that is being done with the held of Threads.

I have two classes as follows:

public class PerformTask
{
    Thread _thread = null;
    public void StartTask()
    {
        _thread = new Thread(new ThreadStart(DoSomeWork));
        _thread.Start();
    }

    public void DoSomeWork()
    {
        // Do Some Work
        _thread = null;
    }

    public void Abort()
    {
        if (_thread != null)
        {
            try
            {
                _thread.Abort();
            }
            catch (ThreadAbortException) {}
        }
    }
}

public class Engine
{
    List<PerformTask> _task = new List<PerformTask>();
    public void Start()
    {
        var task = new PerformTask();
        _task.Add(task);
        // Add task to the timed action queue
        _actionQueue.Add(s => task.StartTask(), TimeSpan.FromSeconds(10));
    }

    public void Stop()
    {
        _task.ForEach(task => task.Abort());
        _task.Clear();
        _actionQueue.Stop();
        _actionQueue.Clear();
    }
}

The _actionQueue is a custom defined source code developed to perform a specified action at a recurring time interval specified. All the actions are kept in queue and invoked at the specified time interval.

Now, the Windows service's OnStart and OnStop method would call Engine class' Start and Stop method respectively.

What I want is when the windows service is stopped, all the threads that are running should stop their processing/execution.

But, what is happening here is as new thread instance is being created in I have a windows service which will start and stop the execution of some process that is being done with the held of Threads.

I have two classes as follows:

public class PerformTask
{
    Thread _thread = null;
    public void StartTask()
    {
        _thread = new Thread(new ThreadStart(DoSomeWork));
        _thread.Start();
    }

    public void DoSomeWork()
    {
        // Do Some Work
        _thread = null;
    }

    public void Abort()
    {
        if (_thread != null)
        {
            try
            {
                _thread.Abort();
            }
            catch (ThreadAbortException) {}
        }
    }
}

public class Engine
{
    List<PerformTask> _task = new List<PerformTask>();
    ActionQueue _actionQueue = new ActionQueue();
    public void Start()
    {
        foreach(.....)
        {
            var task = new PerformTask();
            _task.Add(task);
            // Add task to the timed action queue
            _actionQueue.Add(s => task.StartTask(), TimeSpan.FromSeconds(10));
        }
        _actionQueue.Start();
    }

    public void Stop()
    {
        _task.ForEach(task => task.Abort());
        _task.Clear();
        _actionQueue.Stop();
        _actionQueue.Clear();
    }
}

The ActionQueue is a custom defined source code developed to perform a specified action at a recurring time interval specified. All the actions are kept in queue and invoked at the specified time interval.

Now, the Windows service's OnStart and OnStop method would call Engine class' Start and Stop method respectively.

What I want is when the windows service is stopped, all the threads that are running should stop their processing/execution.

But, what is happening here is as new thread instance is being created in StartTask method, when I call the _task.ForEach(task => task.Abort()) I do not have the correct instance of Thread, that is all the instance of _thread = new Thread(....); is not being accessed, as there would a multiple queues for the same PerformTask class.

Note: I cannot make changes to the ActionQueue.

  1. Is Abort method a correct way of stopping the threads?

  2. How can I stop all the threads (including all the instances of Thread class created by the source code)?

È stato utile?

Soluzione

Usually you'd create a WaitHandle (a ManualResetEvent for example) like that:

ManualResetEvent stopAllThreads = new ManualResetEvent(false);

So the event is "not set". Change the loops in your thread method so that they loop until all work is done or the manual reset event is set.

while (!stopAllThreads.WaitOne(50))

or similar.

Then, in the service's OnStop method, you simply set the event (don't forget to reset it again in OnStart, otherwise the threads will not run again when the service is restarted):

stopAllThreads.Set();

and wait for all the threads to finish.

Actually, aborting threads is not a good way to stop threads - you should always go for something like the above.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top