المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Help with mutex, cannot get mutex to work



C# Programming
05-08-2013, 10:01 PM
This always returns true, whether I have notepad open or not, can anyone tell me what I'm doing wrong?


if (IsSingleInstance()) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(Args)); } else { Process current = Process.GetCurrentProcess(); foreach (Process process in Process.GetProcessesByName(current.ProcessName)) { if (process.Id != current.Id) { SetForegroundWindow(process.MainWindowHandle); break; } } }

static Mutex _m;

[STAThread] static void Main(string[] Args) { static bool IsSingleInstance() { try { // Try to open existing mutex. Mutex.OpenExisting(@"Global\Notepad"); } catch { // If exception occurred, there is no such mutex. Program._m = new Mutex(true, "Notepad"); // Only one instance. return true; } // More than one instance. return false; }}