Skip to content

Commit a6b8321

Browse files
committed
remove task schedulled autostart to use only registry way
1 parent 4597815 commit a6b8321

1 file changed

Lines changed: 29 additions & 72 deletions

File tree

OpenHardwareMonitor/UI/StartupManager.cs

Lines changed: 29 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,20 @@ public StartupManager()
6363

6464
ITaskFolder folder = _scheduler.GetFolder("\\Open Hardware Monitor");
6565
IRegisteredTask task = folder.GetTask("Startup");
66-
_startup = (task != null) &&
67-
(task.Definition.Triggers.Count > 0) &&
68-
(task.Definition.Triggers[1].Type ==
69-
TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) &&
70-
(task.Definition.Actions.Count > 0) &&
71-
(task.Definition.Actions[1].Type ==
72-
TASK_ACTION_TYPE.TASK_ACTION_EXEC) &&
73-
(task.Definition.Actions[1] as IExecAction != null) &&
74-
((task.Definition.Actions[1] as IExecAction).Path ==
75-
Application.ExecutablePath);
76-
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+
}
7780
}
7881
catch (IOException)
7982
{
@@ -126,37 +129,6 @@ public StartupManager()
126129
}
127130
}
128131

129-
private void CreateSchedulerTask()
130-
{
131-
ITaskDefinition definition = _scheduler.NewTask(0);
132-
definition.RegistrationInfo.Description =
133-
"This task starts the Open Hardware Monitor on Windows startup.";
134-
definition.Principal.RunLevel =
135-
TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
136-
definition.Settings.DisallowStartIfOnBatteries = false;
137-
definition.Settings.StopIfGoingOnBatteries = false;
138-
definition.Settings.ExecutionTimeLimit = "PT0S";
139-
_ = (ILogonTrigger)definition.Triggers.Create(TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
140-
IExecAction action = (IExecAction)definition.Actions.Create(TASK_ACTION_TYPE.TASK_ACTION_EXEC);
141-
action.Path = Application.ExecutablePath;
142-
action.WorkingDirectory =
143-
Path.GetDirectoryName(Application.ExecutablePath);
144-
145-
ITaskFolder root = _scheduler.GetFolder("\\");
146-
ITaskFolder folder;
147-
try
148-
{
149-
folder = root.GetFolder("Open Hardware Monitor");
150-
}
151-
catch (IOException)
152-
{
153-
folder = root.CreateFolder("Open Hardware Monitor", "");
154-
}
155-
folder.RegisterTaskDefinition("Startup", definition,
156-
(int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
157-
TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
158-
}
159-
160132
private void DeleteSchedulerTask()
161133
{
162134
ITaskFolder root = _scheduler.GetFolder("\\");
@@ -195,38 +167,23 @@ public bool Startup
195167
}
196168
set
197169
{
198-
if (_startup != value)
170+
if (_startup == value)
171+
return;
172+
173+
if (!IsAvailable)
174+
throw new InvalidOperationException();
175+
176+
try
199177
{
200-
if (IsAvailable)
201-
{
202-
if (_scheduler != null)
203-
{
204-
if (value)
205-
CreateSchedulerTask();
206-
else
207-
DeleteSchedulerTask();
208-
_startup = value;
209-
}
210-
else
211-
{
212-
try
213-
{
214-
if (value)
215-
CreateRegistryRun();
216-
else
217-
DeleteRegistryRun();
218-
_startup = value;
219-
}
220-
catch (UnauthorizedAccessException)
221-
{
222-
throw new InvalidOperationException();
223-
}
224-
}
225-
}
178+
if (value)
179+
CreateRegistryRun();
226180
else
227-
{
228-
throw new InvalidOperationException();
229-
}
181+
DeleteRegistryRun();
182+
_startup = value;
183+
}
184+
catch (UnauthorizedAccessException)
185+
{
186+
throw new InvalidOperationException();
230187
}
231188
}
232189
}

0 commit comments

Comments
 (0)