diff --git a/DS4Windows/Program.cs b/DS4Windows/Program.cs index 61bd3e8..174a204 100644 --- a/DS4Windows/Program.cs +++ b/DS4Windows/Program.cs @@ -15,15 +15,14 @@ namespace DS4Windows [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd); - [DllImport("user32.dll")] - private static extern IntPtr GetForegroundWindow(); // Add "global\" in front of the EventName, then only one instance is allowed on the // whole system, including other users. But the application can not be brought // into view, of course. - private static String SingleAppComEventName = "{a52b5b20-d9ee-4f32-8518-307fa14aa0c6}"; - static Mutex mutex = new Mutex(true, "{FI329DM2-DS4W-J2K2-HYES-92H21B3WJARG}"); + private static string SingleAppComEventName = "{a52b5b20-d9ee-4f32-8518-307fa14aa0c6}"; + //static Mutex mutex = new Mutex(true, "{FI329DM2-DS4W-J2K2-HYES-92H21B3WJARG}"); private static BackgroundWorker singleAppComThread = null; private static EventWaitHandle threadComEvent = null; + private static bool exitComThread = false; public static ControlService rootHub; /// @@ -77,7 +76,8 @@ namespace DS4Windows try { - Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.High; + Process.GetCurrentProcess().PriorityClass = + ProcessPriorityClass.High; } catch { @@ -95,8 +95,8 @@ namespace DS4Windows catch { /* don't care about errors */ } // Create the Event handle - threadComEvent = new EventWaitHandle(false, EventResetMode.AutoReset, SingleAppComEventName); - //CreateInterAppComThread(); + threadComEvent = new EventWaitHandle(false, EventResetMode.ManualReset, SingleAppComEventName); + CreateInterAppComThread(); //if (mutex.WaitOne(TimeSpan.Zero, true)) //{ @@ -104,24 +104,23 @@ namespace DS4Windows Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new DS4Form(args)); - //mutex.ReleaseMutex(); + //mutex.ReleaseMutex(); //} - // End the communication thread. - //singleAppComThread.CancelAsync(); - //threadComEvent.Set(); // signal the other instance. - //while (singleAppComThread.IsBusy) - // Thread.Sleep(50); + exitComThread = true; + threadComEvent.Set(); // signal the other instance. + while (singleAppComThread.IsBusy) + Thread.Sleep(50); threadComEvent.Close(); } static private void CreateInterAppComThread() { singleAppComThread = new BackgroundWorker(); - singleAppComThread.WorkerReportsProgress = false; - singleAppComThread.WorkerSupportsCancellation = true; + //singleAppComThread.WorkerReportsProgress = false; + //singleAppComThread.WorkerSupportsCancellation = true; singleAppComThread.DoWork += new DoWorkEventHandler(singleAppComThread_DoWork); - //singleAppComThread.RunWorkerAsync(); + singleAppComThread.RunWorkerAsync(); } static private void singleAppComThread_DoWork(object sender, DoWorkEventArgs e) @@ -130,15 +129,16 @@ namespace DS4Windows WaitHandle[] waitHandles = new WaitHandle[] { threadComEvent }; Thread.CurrentThread.Priority = ThreadPriority.Lowest; - while (!worker.CancellationPending) + while (!exitComThread) { // check every second for a signal. if (WaitHandle.WaitAny(waitHandles) == 0) { + threadComEvent.Reset(); // The user tried to start another instance. We can't allow that, // so bring the other instance back into view and enable that one. // That form is created in another thread, so we need some thread sync magic. - if (!worker.CancellationPending && Application.OpenForms.Count > 0) + if (!exitComThread && Application.OpenForms.Count > 0) { Form mainForm = Application.OpenForms[0]; mainForm.Invoke(new SetFormVisableDelegate(ThreadFormVisable), mainForm);