Edit main program entry file

using System.Runtime made the biggest difference
This commit is contained in:
Travis Nickles 2018-07-26 02:14:07 -05:00
parent 4cfbc098b0
commit e86113f27b

View File

@ -5,16 +5,13 @@ using System.Runtime.InteropServices;
using System.Diagnostics; using System.Diagnostics;
using System.ComponentModel; using System.ComponentModel;
using System.Globalization; using System.Globalization;
using System.Runtime;
using Microsoft.Win32.TaskScheduler; using Microsoft.Win32.TaskScheduler;
namespace DS4Windows namespace DS4Windows
{ {
static class Program static class Program
{ {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
// 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.
@ -24,6 +21,7 @@ namespace DS4Windows
public static ControlService rootHub; public static ControlService rootHub;
private static Thread testThread; private static Thread testThread;
private static Thread controlThread; private static Thread controlThread;
private static DS4Form ds4form;
/// <summary> /// <summary>
/// The main entry point for the application. /// The main entry point for the application.
@ -71,7 +69,7 @@ namespace DS4Windows
} }
} }
System.Runtime.GCSettings.LatencyMode = System.Runtime.GCLatencyMode.LowLatency; GCSettings.LatencyMode = GCLatencyMode.LowLatency;
try try
{ {
@ -107,7 +105,8 @@ namespace DS4Windows
rootHub.CreateHidGuardKey(); rootHub.CreateHidGuardKey();
//rootHub = new ControlService(); //rootHub = new ControlService();
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.Run(new DS4Form(args)); ds4form = new DS4Form(args);
Application.Run(ds4form);
rootHub.removeHidGuardKey(); rootHub.removeHidGuardKey();
//mutex.ReleaseMutex(); //mutex.ReleaseMutex();
//} //}
@ -131,30 +130,26 @@ namespace DS4Windows
private static void CreateTempWorkerThread() private static void CreateTempWorkerThread()
{ {
testThread = new Thread(singleAppComThread_DoWork); testThread = new Thread(SingleAppComThread_DoWork);
testThread.Priority = ThreadPriority.Lowest; testThread.Priority = ThreadPriority.Lowest;
testThread.IsBackground = true; testThread.IsBackground = true;
testThread.Start(); testThread.Start();
} }
private static void singleAppComThread_DoWork() private static void SingleAppComThread_DoWork()
{ {
//WaitHandle[] waitHandles = new WaitHandle[] { threadComEvent };
while (!exitComThread) while (!exitComThread)
{ {
// check every second for a signal. // check for a signal.
//if (WaitHandle.WaitAny(waitHandles) == 0)
if (threadComEvent.WaitOne()) if (threadComEvent.WaitOne())
{ {
threadComEvent.Reset(); 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 (!exitComThread && Application.OpenForms.Count > 0) if (!exitComThread)
{ {
Form mainForm = Application.OpenForms[0]; ds4form?.Invoke(new SetFormVisableDelegate(ThreadFormVisable), ds4form);
mainForm?.Invoke(new SetFormVisableDelegate(ThreadFormVisable), mainForm);
} }
} }
} }
@ -168,24 +163,13 @@ namespace DS4Windows
private delegate void SetFormVisableDelegate(Form frm); private delegate void SetFormVisableDelegate(Form frm);
static private void ThreadFormVisable(Form frm) static private void ThreadFormVisable(Form frm)
{ {
if (frm != null) if (frm is DS4Form)
{ {
if (frm is DS4Form) // display the form and bring to foreground.
{ frm.WindowState = FormWindowState.Normal;
// display the form and bring to foreground. frm.Focus();
frm.WindowState = FormWindowState.Normal; frm.Activate();
frm.Focus();
}
else
{
WinProgs wp = (WinProgs)frm;
wp.form.mAllowVisible = true;
wp.ShowMainWindow();
SetForegroundWindow(wp.form.Handle);
}
} }
SetForegroundWindow(frm.Handle);
} }
} }
} }