@@ -12,7 +12,7 @@ public static class TaskSchedulerUtils
1212
1313 public static void RunOnBatteryTask ( )
1414 {
15- if ( ! TaskExists ( TASK_NAME_ON_BATTERY ) )
15+ if ( ! TaskAndFolderExists ( TASK_NAME_ON_BATTERY ) )
1616 {
1717 string description = "The GPUPrefSwitcher service runs this task once when the system is considered on battery." +
1818 "\n It is up to you to add actions (e.g. start a script)." ;
@@ -25,7 +25,7 @@ public static void RunOnBatteryTask()
2525
2626 public static void RunPluggedInTask ( )
2727 {
28- if ( ! TaskExists ( TASK_NAME_PLUGGED_IN ) )
28+ if ( ! TaskAndFolderExists ( TASK_NAME_PLUGGED_IN ) )
2929 {
3030 string description = "The GPUPrefSwitcher service runs this task once when the system is considered plugged in." +
3131 "\n It is up to you to add actions (e.g. start a script)." ;
@@ -36,11 +36,28 @@ public static void RunPluggedInTask()
3636 RunTask ( TASK_NAME_PLUGGED_IN ) ;
3737 }
3838
39- public static bool TaskExists ( string taskName )
39+ public static bool TaskAndFolderExists ( string taskName )
4040 {
4141 using ( TaskService taskService = new ( ) )
4242 {
43- return taskService . FindTask ( taskName , false ) != null ;
43+
44+ TaskFolder gpuPrefSwitcherFolder = taskService . GetFolder ( FOLDER_NAME ) ;
45+ if ( gpuPrefSwitcherFolder == null ) return false ;
46+
47+ // Get the folder with the specified name
48+ TaskFolder folder = taskService . RootFolder . SubFolders [ FOLDER_NAME ] ;
49+
50+ // Iterate through the tasks in the folder
51+ foreach ( Task task in folder . Tasks )
52+ {
53+ // Check if the task has the specified name
54+ if ( task . Name . Equals ( taskName , StringComparison . OrdinalIgnoreCase ) )
55+ {
56+ return true ; // Return the task if found
57+ }
58+ }
59+ return false ;
60+ //return taskService.FindTask(taskName, true) != null;
4461 }
4562 }
4663
0 commit comments