Skip to content

Commit f2cdef5

Browse files
committed
Added much better action cleanup - thanks to community user WilliamLeGod for the suggestion to fix the "action executing multiple times" issue!
1 parent 016e307 commit f2cdef5

10 files changed

+34
-3
lines changed

AssistantComputerControl/AssistantComputerControl.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
<DelaySign>false</DelaySign>
9797
</PropertyGroup>
9898
<ItemGroup>
99+
<Reference Include="Microsoft.Win32.TaskScheduler, Version=2.8.20.0, Culture=neutral, PublicKeyToken=c416bc1b32d97233, processorArchitecture=MSIL">
100+
<HintPath>..\packages\TaskScheduler.2.8.20\lib\net452\Microsoft.Win32.TaskScheduler.dll</HintPath>
101+
</Reference>
99102
<Reference Include="Microsoft.WindowsAPICodePack, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
100103
<HintPath>..\packages\Microsoft.WindowsAPICodePack.Core.1.1.0\lib\Microsoft.WindowsAPICodePack.dll</HintPath>
101104
</Reference>

AssistantComputerControl/MainProgram.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
using System.Configuration;
2323
using System.Xml;
2424
using NLog;
25+
using Microsoft.Win32.TaskScheduler;
2526

2627
namespace AssistantComputerControl {
2728
class MainProgram {
2829
public const string softwareVersion = "1.4.2",
29-
releaseDate = "2020-03-14 21:25:00", //YYYY-MM-DD H:i:s - otherwise it gives an error
30+
releaseDate = "2020-08-02 21:25:00", //YYYY-MM-DD H:i:s - otherwise it gives an error
3031
appName = "AssistantComputerControl",
3132

32-
//sentryToken = "super_secret";
33-
sentryToken = "https://[email protected]/1287269"; //Remove on git push
33+
sentryToken = "super_secret";
3434

3535
static public bool debug = true,
3636
unmuteVolumeChange = true,
@@ -340,6 +340,7 @@ void ActualMain() {
340340

341341
//Action mods implementation
342342
ActionMods.CheckMods();
343+
TaskSchedulerSetup();
343344

344345
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch); //On wake up from sleep
345346
Application.Run();
@@ -397,6 +398,31 @@ static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
397398
}
398399
}
399400

401+
public static void TaskSchedulerSetup () {
402+
//Create "Task Scheduler" service; cleanup ACC on startup, log on, workstation unlock
403+
try {
404+
using (TaskService ts = new TaskService()) {
405+
var ps1File = Path.Combine(MainProgram.currentLocation, "ExtraCleanupper.ps1");
406+
407+
TaskDefinition td = ts.NewTask();
408+
td.RegistrationInfo.Author = "Albert MN. | AssistantComputerControl";
409+
td.RegistrationInfo.Description = "AssistantComputerControl cleanup - clears the action folder to prevent the same action being executed twice";
410+
td.Triggers.Add(new BootTrigger());
411+
td.Triggers.Add(new LogonTrigger());
412+
td.Triggers.Add(new SessionStateChangeTrigger { StateChange = TaskSessionStateChangeType.SessionUnlock });
413+
td.Actions.Add(new ExecAction("powershell.exe", $"-WindowStyle Hidden -file \"{ps1File}\" \"{Path.Combine(MainProgram.CheckPath(), "*")}\" \"*.{Properties.Settings.Default.ActionFileExtension}\"", null));
414+
415+
// Register the task in the root folder
416+
ts.RootFolder.RegisterTaskDefinition(@"AssistantComputerControl cleanup", td);
417+
418+
// Remove the task we just created
419+
//ts.RootFolder.DeleteTask("Test");
420+
}
421+
} catch {
422+
DoDebug("Failed to create / update Task Scheduler service");
423+
}
424+
}
425+
400426
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args) {
401427
Exception e = (Exception)args.ExceptionObject;
402428
string errorLogLoc = Path.Combine(dataFolderLocation, "error_log.txt");
@@ -505,6 +531,7 @@ private static bool CreateLogFile() {
505531
public static void SetupListener() {
506532
watcher.Path = CheckPath();
507533
watcher.Filter = "*." + Properties.Settings.Default.ActionFileExtension;
534+
TaskSchedulerSetup();
508535
DoDebug("Listener modified");
509536
}
510537

0 Bytes
Binary file not shown.
Binary file not shown.
167 KB
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

AssistantComputerControl/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
<package id="Sentry.Protocol" version="1.0.4" targetFramework="net461" />
1010
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net461" />
1111
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
12+
<package id="TaskScheduler" version="2.8.20" targetFramework="net461" />
1213
</packages>

0 commit comments

Comments
 (0)