Skip to content

Commit

Permalink
Autostart fixes. (#8)
Browse files Browse the repository at this point in the history
* Fix notifications on autostart minimized before first un-minimize; Fix autostart for multiple users;

* update version number
  • Loading branch information
EricBanker12 authored Aug 21, 2022
1 parent 48e21e0 commit 4598cad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
61 changes: 26 additions & 35 deletions DebugOutputToasts/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
}
}
6 changes: 3 additions & 3 deletions Setup/Setup.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4598cad

Please sign in to comment.