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)]
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;
/// <summary>
@ -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))
//{
@ -107,21 +107,20 @@ namespace DS4Windows
//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);