@@ -41,7 +41,7 @@ public static class Switcher
41
41
/// <summary>
42
42
/// Call once upon service start. Initializes necessary components and starts the timer.
43
43
/// </summary>
44
- internal static void Start ( )
44
+ internal static void Start ( CancellationToken cancellationToken )
45
45
{
46
46
appOptions = new AppOptions ( ) ;
47
47
Logger . inst . EnableRealtimeStandardLogWrites = appOptions . CurrentOptions . EnableRealtimeLogging ;
@@ -70,7 +70,7 @@ internal static void Start()
70
70
71
71
File . Delete ( CRASHED_FILE_PATH ) ; //"successful" initialization yippee
72
72
73
- Task forever = BeginTimerForever ( ) ;
73
+ Task forever = BeginTimerForever ( cancellationToken ) ;
74
74
75
75
/*
76
76
forever.ContinueWith(
@@ -119,15 +119,16 @@ internal static void Stop()
119
119
/// <summary>
120
120
/// There should be only one of this running at any time.
121
121
/// </summary>
122
- private static async Task BeginTimerForever ( )
122
+ private static Task BeginTimerForever ( CancellationToken cancellationToken )
123
123
{
124
124
if ( timerRunning ) { throw new InvalidOperationException ( "This function cannot be called more than once" ) ; }
125
125
timerRunning = true ;
126
- while ( true )
126
+ while ( ! cancellationToken . IsCancellationRequested )
127
127
{
128
128
129
- foreach ( Task task in runningUpdateTasks )
129
+ for ( int i = runningUpdateTasks . Count - 1 ; i >= 0 ; i -- )
130
130
{
131
+ Task task = runningUpdateTasks [ i ] ;
131
132
//TODO run a new task even if the previous one is going
132
133
if ( task != null && task . IsFaulted )
133
134
{
@@ -145,8 +146,10 @@ private static async Task BeginTimerForever()
145
146
146
147
runningUpdateTasks . Add ( RunUpdateLogic ( ) ) ;
147
148
148
- await Task . Delay ( updateInterval ) ; //move this to the beginning to make debugging easier
149
+ Task . Delay ( updateInterval ) . Wait ( ) ; //move this to the beginning to make debugging easier
149
150
}
151
+
152
+ return Task . CompletedTask ;
150
153
}
151
154
152
155
private static async Task RunUpdateLogic ( )
0 commit comments