diff --git a/DebugOutputToasts/MainWindow.xaml.cs b/DebugOutputToasts/MainWindow.xaml.cs index 69e777d..dfb333a 100644 --- a/DebugOutputToasts/MainWindow.xaml.cs +++ b/DebugOutputToasts/MainWindow.xaml.cs @@ -30,7 +30,6 @@ public partial class MainWindow : Window private Task FilterTask = null; private Task NotifyTask = null; - private Task RefreshTask = null; private CancellationTokenSource FilterCancel = null; private CancellationTokenSource NotifyCancel = null; @@ -87,10 +86,7 @@ public MainWindow() // "-m" to start minimized if (args.Contains("-m")) - { this.WindowState = WindowState.Minimized; - Window_Deactivated(null, null); - } // Update TaskScheduler SetTaskScheduler(Config.StartWithLogin); @@ -143,22 +139,8 @@ private void StackPanel_Loaded_MessagePanel(object sender, RoutedEventArgs e) // create monitor Monitor = new DebugOutputMonitor(DebugOutputHandler); - //// Refresh monitor every 12 hours, a workaround for unknown crashing on long running instance. - //if (RefreshCancel != null && !RefreshCancel.IsCancellationRequested) - // RefreshCancel.Cancel(); - - //RefreshCancel = new CancellationTokenSource(); - //RefreshTask = new Task(async () => - //{ - // while (!RefreshCancel.IsCancellationRequested) - // { - // Monitor.Dispose(); - // Monitor = new DebugOutputMonitor(DebugOutputHandler); - // await Task.Delay(TimeSpan.FromHours(12)); - // } - //}, RefreshCancel.Token); - - //RefreshTask.Start(TaskScheduler.FromCurrentSynchronizationContext()); + if (this.WindowState == WindowState.Minimized) + Window_Deactivated(null, null); } private void StackPanel_Unloaded_MessagePanel(object sender, RoutedEventArgs e) @@ -512,6 +494,16 @@ private void TextBox_TextChanged_Replace(object sender, TextChangedEventArgs e) Nett.Toml.WriteFile(Config, ConfigPath); } + + private void Window_Deactivated(object sender, EventArgs e) + { + if (Config.MinimizeToTrayIcon && this.WindowState == WindowState.Minimized) + { + this.Hide(); + NotifyIcon.Visible = true; + //NotifyIcon.ShowBalloonTip(5000, "Minimized to a tray icon", "Left-click to show. Right-click for options.", System.Windows.Forms.ToolTipIcon.None); + } + } #endregion #region Event Helpers @@ -1017,14 +1009,13 @@ private void SetTaskScheduler(bool enable) if (task == null) { var def = ts.NewTask(); - def.Settings.Enabled = enable; def.Settings.DisallowStartIfOnBatteries = false; def.Settings.StopIfGoingOnBatteries = false; def.Settings.ExecutionTimeLimit = TimeSpan.Zero; - + var trigger = new LogonTrigger(); - //taskTrigger.ExecutionTimeLimit = TimeSpan.Zero; trigger.UserId = Environment.UserName; + trigger.Enabled = enable; def.Triggers.Add(trigger); var action = new ExecAction(); @@ -1037,26 +1028,26 @@ private void SetTaskScheduler(bool enable) } else { + var userId = $"{Environment.UserDomainName}\\{Environment.UserName}"; + var trigger = (LogonTrigger)task.Definition.Triggers.Where(t => ((LogonTrigger)t).UserId == userId).FirstOrDefault(); + if (trigger == default(LogonTrigger)) + { + trigger = new LogonTrigger(); + trigger.UserId = userId; + task.Definition.Triggers.Add(trigger); + } + + trigger.Enabled = enable; + task.Definition.Settings.Enabled = true; // fix old tasks after moving "enabled" from task.Definition.Settings to task.Definition.Triggers + var action = (ExecAction)task.Definition.Actions.First(); action.Path = System.Reflection.Assembly.GetExecutingAssembly().Location; action.WorkingDirectory = Path.GetDirectoryName(action.Path); - task.Definition.Settings.Enabled = enable; - task.RegisterChanges(); } } } #endregion - - private void Window_Deactivated(object sender, EventArgs e) - { - if (Config.MinimizeToTrayIcon && this.WindowState == WindowState.Minimized) - { - this.Hide(); - NotifyIcon.Visible = true; - //NotifyIcon.ShowBalloonTip(5000, "Minimized to a tray icon", "Left-click to show. Right-click for options.", System.Windows.Forms.ToolTipIcon.None); - } - } } } diff --git a/Setup/Setup.vdproj b/Setup/Setup.vdproj index 2d85e00..ec286a3 100644 --- a/Setup/Setup.vdproj +++ b/Setup/Setup.vdproj @@ -12440,15 +12440,15 @@ { "Name" = "8:Microsoft Visual Studio" "ProductName" = "8:DebugOutputToasts" - "ProductCode" = "8:{47A9ED8A-832E-4BB0-AFA4-2F099751E182}" - "PackageCode" = "8:{7A36D3FB-0815-4A09-A6A2-7ABA71AAC124}" + "ProductCode" = "8:{F523DC36-F5D9-48D5-9C1F-B1B6326680F1}" + "PackageCode" = "8:{045F297C-FF61-450E-8ED0-39F8B479C42C}" "UpgradeCode" = "8:{2F8901F2-8C94-4C69-93F5-CC1B9B712A58}" "AspNetVersion" = "8:" "RestartWWWService" = "11:FALSE" "RemovePreviousVersions" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE" "InstallAllUsers" = "11:FALSE" - "ProductVersion" = "8:1.1.2" + "ProductVersion" = "8:1.1.3" "Manufacturer" = "8:Eric Banker" "ARPHELPTELEPHONE" = "8:" "ARPHELPLINK" = "8:https://github.com/EricBanker12/DebugOutputToasts/issues"