app: move pinging loop to daemon thread

This commit is contained in:
Nguyen Duy
2024-08-29 19:58:05 +07:00
parent b35ed9f799
commit 80bd3c4db0
4 changed files with 47 additions and 10 deletions
+42
View File
@@ -0,0 +1,42 @@
using System;
using System.Threading;
namespace ParsecVDisplay
{
internal static class Daemon
{
static Thread EventThread;
static CancellationTokenSource Cancellation;
public static void Start()
{
Cancellation = new CancellationTokenSource();
var token = Cancellation.Token;
EventThread = new Thread(() => EventLoop(token));
EventThread.IsBackground = false;
EventThread.Priority = ThreadPriority.Highest;
EventThread.Start();
}
public static void Stop()
{
Cancellation?.Cancel();
EventThread?.Join();
}
static void EventLoop(CancellationToken cancellation)
{
while (true)
{
if (cancellation.IsCancellationRequested)
break;
ParsecVDD.Ping();
Thread.Sleep(100);
}
}
}
}
+1
View File
@@ -89,6 +89,7 @@
<DependentUpon>DisplayItem.xaml</DependentUpon>
</Compile>
<Compile Include="Config.cs" />
<Compile Include="Daemon.cs" />
<Compile Include="Device.cs" />
<Compile Include="Display.cs" />
<Compile Include="Helper.cs" />
+4
View File
@@ -45,7 +45,11 @@ namespace ParsecVDisplay
if (InitDriver())
{
Helper.StayAwake(false);
Daemon.Start();
Application.Run(new Tray());
Daemon.Stop();
ParsecVDD.Uninit();
}
}
-10
View File
@@ -16,7 +16,6 @@ namespace ParsecVDisplay
static System.Windows.Controls.ContextMenu Menu;
Thread AppThread;
System.Windows.Forms.Timer VddTimer;
ToolStripMenuItem MI_RunOnStartup;
ToolStripMenuItem MI_RestoreDisplays;
@@ -46,11 +45,6 @@ namespace ParsecVDisplay
AppThread.SetApartmentState(ApartmentState.STA);
AppThread.Start();
VddTimer = new System.Windows.Forms.Timer();
VddTimer.Interval = 50;
VddTimer.Tick += VddTimer_Tick;
VddTimer.Start();
var appName = Program.AppName;
var appIcon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
@@ -281,9 +275,6 @@ namespace ParsecVDisplay
Config.DisplayCount = displays.Count;
}
VddTimer.Tick -= VddTimer_Tick;
VddTimer.Stop();
App.Current?.Dispatcher.Invoke(App.Current.Shutdown);
AppThread.Join();
@@ -295,7 +286,6 @@ namespace ParsecVDisplay
{
if (disposing)
{
VddTimer.Dispose();
TrayIcon.Dispose();
}