diff --git a/app/Daemon.cs b/app/Daemon.cs new file mode 100644 index 0000000..c071ef9 --- /dev/null +++ b/app/Daemon.cs @@ -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); + } + } + } +} diff --git a/app/ParsecVDisplay.csproj b/app/ParsecVDisplay.csproj index 557398e..4b43e4b 100644 --- a/app/ParsecVDisplay.csproj +++ b/app/ParsecVDisplay.csproj @@ -89,6 +89,7 @@ DisplayItem.xaml + diff --git a/app/Program.cs b/app/Program.cs index 6db86e1..d08d933 100644 --- a/app/Program.cs +++ b/app/Program.cs @@ -45,7 +45,11 @@ namespace ParsecVDisplay if (InitDriver()) { Helper.StayAwake(false); + Daemon.Start(); + Application.Run(new Tray()); + + Daemon.Stop(); ParsecVDD.Uninit(); } } diff --git a/app/Tray.cs b/app/Tray.cs index f77d81f..207eec2 100644 --- a/app/Tray.cs +++ b/app/Tray.cs @@ -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(); }