My situation is that i am creating multiple threads for performing a job. For instance you can say that "Job is to process some data." Now as soon as the user press "Stop" button, i want to abort all these threads and update the status of job in database.
internal void CreateJobThread(Job job)
{
Interlocked.Increment(ref _threadCounter);
ThreadPool.QueueUserWorkItem(StartJobThread, job);
}
StartJobThread is the function which is doing job.
internal void StartJobThread(object input)
{
try
{
//Do something
}
catch (Exception ex)
{
//Catch exception
}
finally
{
Interlocked.Decrement(ref _threadCounter);
}
}
Please let me know how can i cancel all of the threads currently running. And how can i get the IDs of jobs in progress. So that i can update their status in database.