I am currently working on a C # application that runs in an infinite loop with a Thread.Sleep call after each iteration of the other method calls. My main
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 inserted a KeepAlive call at the end of the method to make sure that the singleton mutex works as expected, as described on different websites. The KeepAlive call must contain garbage collection in order to drop mutexes, since .NET is looking forward to anticipating / optimizing garbage collection.
, , KeepAlive , Thread.Sleep? , KeepAlive , , .