I noticed in my Task Manager I have several copies of this app - though not take any CPU resources.
I know I must be doing something wrong, so I ask the collective...
Here is the sterilized code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace AnalyticsAggregator
{
class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
bool onlyInstance = false;
Mutex mutex = new Mutex(true, "AnalyticsAggregator", out onlyInstance);
if (!onlyInstance)
{
return;
}
"Do stuff with the database"
GC.KeepAlive(mutex);
}
catch (Exception e)
{
EventLog eventlog = new EventLog("Application");
eventlog.Source = "AnalyticsAggregator";
eventlog.WriteEntry(e.Message, EventLogEntryType.Error);
}
}
}
}
}
I have other console apps that are not mutex/singleton that exhibit the same behavior, what am I doing wrong? I am assuming some type of disposal...
Thanks
GC.KeepAlive(mutex)in there?"Do Stuff"line with a longThread.Sleep(100000). It worked as expected, where only one instance of the application would be running. Can you post code from which the problem can be reproduced, as well as more information about your environment?GC.KeepAlive(object)(msdn.microsoft.com/en-us/library/…) it looks like you should only use it if you are calling unmanaged code that might hang on to the specified object. Are you using any COM code or unmanaged DLL's in the "Do stuff with the database" code?