Skip to content

Commit

Permalink
Task Scheduler and Options fixes (#95)
Browse files Browse the repository at this point in the history
* Enable / disable the scheduled task according to the chosen settings

* Renamed "Save Settings" in Options window to "OK" and it now closes the window after saving settings.
  • Loading branch information
DavidMoore committed Aug 18, 2023
1 parent f704520 commit 704da3d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
20 changes: 19 additions & 1 deletion Code/IPFilter/Commands/ScheduledTaskCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,29 @@ public enum _TASK_RUNLEVEL

const string taskPath = "IPFilter";

public static void Execute()
public static void Execute(bool enable)
{
var type = Type.GetTypeFromProgID("Schedule.Service");
dynamic service = Activator.CreateInstance(type);

service.Connect();

if(!enable)
{
Trace.TraceInformation("Deleting the automatic schedule task...");
var folder = service.GetFolder("\\");
var existingTask = folder.GetTask(taskPath);
if (existingTask != null)
{
existingTask.Enabled = false;
existingTask.Stop(0);
folder.DeleteTask(taskPath, 0);
Trace.TraceInformation("Successfully deleted the scheduled task.");
return;
}
Trace.TraceInformation("No task found to delete.");
return;
}

Trace.TraceInformation("Setting up the automatic schedule...");
var task = service.NewTask(0);
Expand Down Expand Up @@ -92,6 +109,7 @@ public static void Execute()
task.Settings.RunOnlyIfNetworkAvailable = true;
task.Settings.StartWhenAvailable = true;
task.Settings.WakeToRun = false;
task.Settings.Enabled = true;

task.Principal.RunLevel = _TASK_RUNLEVEL.TASK_RUNLEVEL_LUA;
task.Principal.LogonType = _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN;
Expand Down
2 changes: 1 addition & 1 deletion Code/IPFilter/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal static void Main(string[] args)
{
try
{
ScheduledTaskCommand.Execute();
ScheduledTaskCommand.Execute(true);
}
catch (Exception ex)
{
Expand Down
7 changes: 6 additions & 1 deletion Code/IPFilter/ViewModels/OptionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace IPFilter.ViewModels
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows;
using Services;

public class OptionsViewModel : INotifyPropertyChanged
Expand Down Expand Up @@ -101,7 +102,7 @@ void SaveSettings(object o)
try
{
Trace.TraceInformation("Updating schedule settings...");
Commands.ScheduledTaskCommand.Execute();
Commands.ScheduledTaskCommand.Execute(Config.Default.settings.task.isEnabled);
}
catch (UnauthorizedAccessException)
{
Expand All @@ -112,7 +113,11 @@ void SaveSettings(object o)
{
Trace.TraceError("Couldn't schedule automated update: " + ex);
ErrorMessage = "Couldn't schedule automated update: " + ex.Message;
return;
}

if (o is not Window window) return;
window.Close();
}

public DelegateCommand SaveSettingsCommand { get; private set; }
Expand Down
2 changes: 1 addition & 1 deletion Code/IPFilter/Views/OptionsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem >
<Button Padding="5" Margin="5" Content="Save Settings" Command="{Binding SaveSettingsCommand}"/>
<Button Padding="5" Margin="5" Content="OK" Command="{Binding SaveSettingsCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</StatusBarItem>
<StatusBarItem>
<Label Content="{Binding ErrorMessage}" Foreground="Red" Width="Auto"/>
Expand Down

0 comments on commit 704da3d

Please sign in to comment.