Program init changes

This commit is contained in:
Travis Nickles 2017-07-03 08:31:58 -07:00
parent 001f2e5432
commit 9bf937d7b9

View File

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