Skip to content

Commit fbe2114

Browse files
committed
TaskScheduler autostart restored
1 parent cbf0cdf commit fbe2114

2 files changed

Lines changed: 101 additions & 25 deletions

File tree

OpenHardwareMonitor/OpenHardwareMonitor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<PrivateAssets>all</PrivateAssets>
2929
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3030
</PackageReference>
31+
<PackageReference Include="TaskScheduler" Version="2.11.0" />
3132
<PackageReference Include="System.Management" Version="9.0.0" />
3233
</ItemGroup>
3334
<ItemGroup>

OpenHardwareMonitor/UI/StartupManager.cs

Lines changed: 100 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
// All Rights Reserved.
66

77
using System;
8+
using System.IO;
9+
using System.Linq;
810
using System.Security;
911
using System.Security.Principal;
1012
using System.Windows.Forms;
1113
using Microsoft.Win32;
14+
using Microsoft.Win32.TaskScheduler;
15+
using Action = Microsoft.Win32.TaskScheduler.Action;
1216

1317
namespace OpenHardwareMonitor.UI;
1418

@@ -25,21 +29,41 @@ public StartupManager()
2529
return;
2630
}
2731

28-
try
32+
if (IsAdministrator() && TaskService.Instance.Connected)
2933
{
30-
using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(RegistryPath))
31-
{
32-
string value = (string)registryKey?.GetValue(nameof(OpenHardwareMonitor));
34+
IsAvailable = true;
3335

34-
if (value != null)
35-
_startup = value == Application.ExecutablePath;
36+
Task task = GetTask();
37+
if (task != null)
38+
{
39+
foreach (Action action in task.Definition.Actions)
40+
{
41+
if (action.ActionType == TaskActionType.Execute && action is ExecAction execAction)
42+
{
43+
if (execAction.Path.Equals(Application.ExecutablePath, StringComparison.OrdinalIgnoreCase))
44+
_startup = true;
45+
}
46+
}
3647
}
37-
38-
IsAvailable = true;
3948
}
40-
catch (SecurityException)
49+
else
4150
{
42-
IsAvailable = false;
51+
try
52+
{
53+
using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(RegistryPath))
54+
{
55+
string value = (string)registryKey?.GetValue(nameof(OpenHardwareMonitor));
56+
57+
if (value != null)
58+
_startup = value == Application.ExecutablePath;
59+
}
60+
61+
IsAvailable = true;
62+
}
63+
catch (SecurityException)
64+
{
65+
IsAvailable = false;
66+
}
4367
}
4468
}
4569

@@ -50,29 +74,41 @@ public bool Startup
5074
get { return _startup; }
5175
set
5276
{
53-
if (_startup == value)
54-
return;
55-
56-
if (IsAvailable)
77+
if (_startup != value)
5778
{
58-
try
79+
if (IsAvailable)
5980
{
60-
if (value)
61-
CreateRegistryKey();
81+
if (TaskService.Instance.Connected)
82+
{
83+
if (value)
84+
CreateTask();
85+
else
86+
DeleteTask();
87+
88+
_startup = value;
89+
}
6290
else
63-
DeleteRegistryKey();
91+
{
92+
try
93+
{
94+
if (value)
95+
CreateRegistryKey();
96+
else
97+
DeleteRegistryKey();
6498

65-
_startup = value;
99+
_startup = value;
100+
}
101+
catch (UnauthorizedAccessException)
102+
{
103+
throw new InvalidOperationException();
104+
}
105+
}
66106
}
67-
catch (UnauthorizedAccessException)
107+
else
68108
{
69109
throw new InvalidOperationException();
70110
}
71111
}
72-
else
73-
{
74-
throw new InvalidOperationException();
75-
}
76112
}
77113
}
78114

@@ -91,6 +127,45 @@ private static bool IsAdministrator()
91127
}
92128
}
93129

130+
private static Task GetTask()
131+
{
132+
try
133+
{
134+
return TaskService.Instance.AllTasks.FirstOrDefault(x => x.Name.Equals(nameof(OpenHardwareMonitor), StringComparison.OrdinalIgnoreCase));
135+
}
136+
catch
137+
{
138+
return null;
139+
}
140+
}
141+
142+
private void CreateTask()
143+
{
144+
TaskDefinition taskDefinition = TaskService.Instance.NewTask();
145+
taskDefinition.RegistrationInfo.Description = "Starts OpenHardwareMonitor on Windows startup.";
146+
147+
taskDefinition.Triggers.Add(new LogonTrigger());
148+
149+
taskDefinition.Settings.StartWhenAvailable = true;
150+
taskDefinition.Settings.DisallowStartIfOnBatteries = false;
151+
taskDefinition.Settings.StopIfGoingOnBatteries = false;
152+
taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.Zero;
153+
taskDefinition.Settings.AllowHardTerminate = false;
154+
155+
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
156+
taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
157+
158+
taskDefinition.Actions.Add(new ExecAction(Application.ExecutablePath, "", Path.GetDirectoryName(Application.ExecutablePath)));
159+
160+
TaskService.Instance.RootFolder.RegisterTaskDefinition(nameof(OpenHardwareMonitor), taskDefinition);
161+
}
162+
163+
private static void DeleteTask()
164+
{
165+
Task task = GetTask();
166+
task?.Folder.DeleteTask(task.Name, false);
167+
}
168+
94169
private static void CreateRegistryKey()
95170
{
96171
RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(RegistryPath);
@@ -102,4 +177,4 @@ private static void DeleteRegistryKey()
102177
RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(RegistryPath);
103178
registryKey?.DeleteValue(nameof(OpenHardwareMonitor));
104179
}
105-
}
180+
}

0 commit comments

Comments
 (0)