I have a console app in C# that runs endlessly and looks something like this:
class Program
{
static void Main(string[] args)
{
while(true)
{
var listOfIds = GetItemIds();
Parallel.ForEach(listOfIds,
new Parallel { MaxDegreeOfParallelism = 15}, th => DoWork(th));
Console.WriteLine("Iteration Complete");
}
}
static void DoWork(int id)
{
//do some work and save data
}
static List<int> GetItemIds()
{
//return a list of ints
}
}
While a thread is in DoWork
, it is processing data, modifying it and storing it. The app waits for all to finish and then goes again for a new iteration.
Now, if the console app is closed, it seems that threads do not finish their work and die with the console app. I thought threads created with Parallel
were independent of the main thread. Is there a way to make the threads finish their work even if I exit the console app? should I use something different rather than Parallel
to create the threads?