I am currently working on an application in C# that runs on an infinite loop with a Thread.Sleep call after each iteration of the other method calls. My main is -
static void Main(string[] args)
{
bool isOnlyInstance = false;
Mutex mutex = new Mutex(true, "RiskMetricsSensitivitiesDatabaseLoader", out isOnlyInstance);
if (!isOnlyInstance)
{
return;
}
while (true)
{
ProcessData();
Thread.Sleep(MainLoopSleep);
}
GC.KeepAlive(mutex);
}
I have inserted the KeepAlive call at the end of the method to ensure the singleton mutex works as expected, as outlined by various websites. The call to KeepAlive is supposed to keep garbage collection from throwing away the mutex, since .NET looks forward to anticipate/optimize garbage collection.
My question is, since the actual call to KeepAlive never gets reached, should I put it in the loop after Thread.Sleep? The compiler warns that KeepAlive never gets called, and I'm concerned that it will therefore ignore this line in my garbage collection prevention algorithm.