The number of machines running this isn't proof enough. If you have a
minimal number of processes on each machine, it doesn't matter how many
machines are running the app.
In the end, the process approach can take up to O(n) time, where n is
the number of processes on the machine. The mutex approach is always an
O(1) operation. Always guaranteed to be more efficient.
The Mutex approach is easy:
public static void Main()
{
// Created?
bool created = false;
// Create the mutex.
using (Mutex mutex = new Mutex(true, "MyMutex", out created))
{
// If this is not created, get out.
if (!created)
{
// Get out.
return;
}
// Run your app code here.
}
}
If you want your running app to handle command line parameters that are
passed to new instances that are run, then you should look at the
WindowsFormsApplicationBase class in the Microsoft.VisualBasic namespace,
as it handles this for you.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Dan Tallent said:
Can you supply the sample code for the mutex approach?
BTW, this method works fine for TS and XP. I have 15 machines in my
building alone as proof.
Dan