I started multiple threads by for each loop in my windows service.I didn’t mention any name for these threads.How can i terminate these thread.
I use the following code to create threads.
new Thread(() =>
{
foreach (MyClass detail in MyclassList)
{
DoWork(detail);
}
}).Start();
By starting these threads i create a schedule task for each details.Can i dispose this thread after Scheduling is done and How?
Forcibly terminating threads is never a good idea. What you should really be doing is putting some suitably designed “should I continue” check inside the loop, that you can set to “no” externally. How to do that depends a bit more on context… For example:
There are, however, a myriad of ways to sort out a thread-safe exit test; the only slightly tricky thing is keeping hold of some kind of context with access to the thing being checked.