mirror of
https://github.com/cemu-project/DS4Windows.git
synced 2024-11-23 09:49:16 +01:00
Program init changes
This commit is contained in:
parent
001f2e5432
commit
9bf937d7b9
@ -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))
|
||||||
//{
|
//{
|
||||||
@ -107,21 +107,20 @@ namespace DS4Windows
|
|||||||
//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);
|
||||||
|
Loading…
Reference in New Issue
Block a user