@@ -329,22 +329,15 @@ public async void StopMacro(string macroId)
329329 if ( _macroStates . TryGetValue ( macroId , out var state ) )
330330 {
331331 state . CancellationSource . Cancel ( ) ;
332- state . Macro . StateChanged -= OnMacroStateChanged ;
333332 state . Macro . State = MacroState . Completed ;
334333
334+ // rest of the cleanup will be handled by OnMacroStateChanged
335335 UnregisterFunctionTriggers ( state . Macro ) ;
336336 await SetPluginStates ( state . Macro , true ) ;
337337
338- // Execute cleanup functions
339- _cleanupManager . ExecuteCleanup ( macroId , "Stopped" ) ;
340-
341338 if ( C . PropagateControlsToChildren )
342339 foreach ( var child in _macroHierarchy . GetChildMacros ( macroId ) . ToList ( ) )
343340 StopMacro ( child . Id ) ;
344-
345- if ( _macroStates . TryRemove ( macroId , out var removedState ) )
346- removedState . Dispose ( ) ;
347- _enginesByMacroId . TryRemove ( macroId , out _ ) ;
348341 }
349342 }
350343
@@ -505,8 +498,15 @@ private void OnMacroStateChanged(object? sender, MacroStateChangedEventArgs e)
505498 tempMacro . StateChanged -= OnMacroStateChanged ;
506499 }
507500
508- StopMacro ( e . MacroId ) ; // handle local cancellations/state
509- CleanupMacro ( e . MacroId ) ; // cleanup triggers/state
501+ if ( _macroStates . TryRemove ( e . MacroId , out var state ) )
502+ {
503+ UnregisterFunctionTriggers ( state . Macro ) ;
504+ state . CancellationSource . Cancel ( ) ;
505+ state . CancellationSource . Dispose ( ) ;
506+ state . Macro . StateChanged -= OnMacroStateChanged ;
507+ }
508+
509+ _enginesByMacroId . TryRemove ( e . MacroId , out _ ) ;
510510 }
511511 }
512512
0 commit comments