If you want to activate only one instance(.exe) of your application use this code in C#
using System.Diagnostics;
public static Process PriorProcess()
// Returns a System.Diagnostics.Process pointing to
// a pre-existing process with the same name as the
// current one, if any; or null if the current process
// is unique.
{
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs)
{
if ((p.Id != curr.Id) &&
(p.MainModule.FileName == curr.MainModule.FileName))
return p;
}
return null;
}
.
.
.
[STAThread]
static void Main() // args are OK here, of course
{
if (PriorProcess() != null)
{
MessageBox("Another instance is already running.");
return;
}
Application.Run(new Form1()); // or whatever was here
}
Sunday, May 3, 2009
Ensuring that only a single instance of a .NET application is running
Subscribe to:
Post Comments (Atom)
0 תגובות:
Post a Comment