Skip to content

Commit 954328f

Browse files
committed
fixup! remove task schedulled autostart to use only registry way
1 parent 4a117ce commit 954328f

1 file changed

Lines changed: 40 additions & 84 deletions

File tree

OpenHardwareMonitor/UI/StartupManager.cs

Lines changed: 40 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.InteropServices;
43
using System.Security;
54
using System.Security.Principal;
65
using System.Windows.Forms;
@@ -11,15 +10,14 @@ namespace OpenHardwareMonitor.UI
1110
public class StartupManager
1211
{
1312

14-
private readonly TaskSchedulerClass _scheduler;
1513
private bool _startup;
1614
private const string REGISTRY_RUN = @"Software\Microsoft\Windows\CurrentVersion\Run";
1715

1816
private bool IsAdministrator()
1917
{
2018
try
2119
{
22-
WindowsIdentity identity = WindowsIdentity.GetCurrent();
20+
var identity = WindowsIdentity.GetCurrent();
2321
WindowsPrincipal principal = new(identity);
2422
return principal.IsInRole(WindowsBuiltInRole.Administrator);
2523
}
@@ -33,7 +31,6 @@ public StartupManager()
3331
{
3432
if (Software.OperatingSystem.IsUnix)
3533
{
36-
_scheduler = null;
3734
IsAvailable = false;
3835
return;
3936
}
@@ -42,99 +39,58 @@ public StartupManager()
4239
{
4340
try
4441
{
45-
_scheduler = new TaskSchedulerClass();
46-
_scheduler.Connect(null, null, null, null);
42+
var _scheduler = new TaskSchedulerClass();
43+
_scheduler.Connect();
44+
45+
var folder = _scheduler.GetFolder("\\Open Hardware Monitor");
46+
var task = folder.GetTask("Startup");
47+
_startup = task != null &&
48+
task.Definition.Triggers.Count > 0 &&
49+
task.Definition.Triggers[1].Type == TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON &&
50+
task.Definition.Actions.Count > 0 &&
51+
task.Definition.Actions[1].Type == TASK_ACTION_TYPE.TASK_ACTION_EXEC &&
52+
task.Definition.Actions[1] is IExecAction execAction &&
53+
execAction.Path == Application.ExecutablePath;
54+
55+
if (_startup)
56+
{
57+
//old versions compatibility - convert task to registry
58+
DeleteSchedulerTask(_scheduler);
59+
CreateRegistryRun();
60+
}
4761
}
4862
catch
4963
{
50-
_scheduler = null;
51-
}
52-
53-
if (_scheduler != null)
54-
{
55-
try
56-
{
57-
try
58-
{
59-
// check if the taskscheduler is running
60-
IRunningTaskCollection collection = _scheduler.GetRunningTasks(0);
61-
}
62-
catch (ArgumentException) { }
63-
64-
ITaskFolder folder = _scheduler.GetFolder("\\Open Hardware Monitor");
65-
IRegisteredTask task = folder.GetTask("Startup");
66-
_startup = task != null &&
67-
task.Definition.Triggers.Count > 0 &&
68-
task.Definition.Triggers[1].Type == TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON &&
69-
task.Definition.Actions.Count > 0 &&
70-
task.Definition.Actions[1].Type == TASK_ACTION_TYPE.TASK_ACTION_EXEC &&
71-
task.Definition.Actions[1] is IExecAction execAction &&
72-
execAction.Path == Application.ExecutablePath;
73-
74-
if (_startup)
75-
{
76-
//old versions compatibility - convert task to registry
77-
DeleteSchedulerTask();
78-
CreateRegistryRun();
79-
}
80-
}
81-
catch (IOException)
82-
{
83-
_startup = false;
84-
}
85-
catch (UnauthorizedAccessException)
86-
{
87-
_scheduler = null;
88-
}
89-
catch (COMException)
90-
{
91-
_scheduler = null;
92-
}
93-
catch (NotImplementedException)
94-
{
95-
_scheduler = null;
96-
}
64+
_startup = false;
9765
}
9866
}
99-
else
100-
{
101-
_scheduler = null;
102-
}
10367

104-
if (_scheduler == null)
68+
try
10569
{
106-
try
70+
using (var key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN))
10771
{
108-
using (RegistryKey key =
109-
Registry.CurrentUser.OpenSubKey(REGISTRY_RUN))
72+
_startup = false;
73+
if (key != null)
11074
{
111-
_startup = false;
112-
if (key != null)
113-
{
114-
string value = (string)key.GetValue("OpenHardwareMonitor");
115-
if (value != null)
116-
_startup = value == Application.ExecutablePath;
117-
}
75+
var value = (string)key.GetValue("OpenHardwareMonitor");
76+
if (value != null)
77+
_startup = value == Application.ExecutablePath;
11878
}
119-
IsAvailable = true;
120-
}
121-
catch (SecurityException)
122-
{
123-
IsAvailable = false;
12479
}
80+
IsAvailable = true;
12581
}
126-
else
82+
catch (SecurityException)
12783
{
128-
IsAvailable = true;
84+
IsAvailable = false;
12985
}
13086
}
13187

132-
private void DeleteSchedulerTask()
88+
private static void DeleteSchedulerTask(TaskSchedulerClass scheduler)
13389
{
134-
ITaskFolder root = _scheduler.GetFolder("\\");
90+
var root = scheduler.GetFolder("\\");
13591
try
13692
{
137-
ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
93+
var folder = root.GetFolder("Open Hardware Monitor");
13894
folder.DeleteTask("Startup", 0);
13995
}
14096
catch (IOException) { }
@@ -145,16 +101,16 @@ private void DeleteSchedulerTask()
145101
catch (IOException) { }
146102
}
147103

148-
private void CreateRegistryRun()
104+
private static void CreateRegistryRun()
149105
{
150-
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
151-
key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
106+
var key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
107+
key?.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
152108
}
153109

154-
private void DeleteRegistryRun()
110+
private static void DeleteRegistryRun()
155111
{
156-
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
157-
key.DeleteValue("OpenHardwareMonitor");
112+
var key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
113+
key?.DeleteValue("OpenHardwareMonitor");
158114
}
159115

160116
public bool IsAvailable { get; }

0 commit comments

Comments
 (0)