@@ -77,10 +77,10 @@ public string QueueCommand(string command)
7777 {
7878 if ( m_disposed )
7979 throw new ObjectDisposedException ( nameof ( CommandQueueService ) ) ;
80-
80+
8181 if ( string . IsNullOrWhiteSpace ( command ) )
8282 throw new ArgumentException ( "Command cannot be null or empty" , nameof ( command ) ) ;
83-
83+
8484 var commandId = Guid . NewGuid ( ) . ToString ( ) ;
8585 m_logger . LogInformation ( "🔄 QueueCommand START: {CommandId} for command: {Command}" , commandId , command ) ;
8686
@@ -108,10 +108,10 @@ public Task<string> GetCommandResult(string commandId)
108108 {
109109 if ( m_disposed )
110110 throw new ObjectDisposedException ( nameof ( CommandQueueService ) ) ;
111-
111+
112112 if ( string . IsNullOrEmpty ( commandId ) )
113113 return Task . FromResult ( $ "Command not found: { commandId } ") ;
114-
114+
115115 // NOTE: Completed commands stay in m_activeCommands for result retrieval
116116 if ( m_activeCommands . TryGetValue ( commandId , out var command ) )
117117 {
@@ -142,10 +142,10 @@ public bool CancelCommand(string commandId)
142142 {
143143 if ( m_disposed )
144144 throw new ObjectDisposedException ( nameof ( CommandQueueService ) ) ;
145-
145+
146146 if ( string . IsNullOrEmpty ( commandId ) )
147147 return false ;
148-
148+
149149 // First check if command is in active commands (executing or completed)
150150 if ( m_activeCommands . TryGetValue ( commandId , out var command ) )
151151 {
@@ -180,7 +180,7 @@ public bool CancelCommand(string commandId)
180180 if ( queuedCommand != null )
181181 {
182182 m_logger . LogInformation ( "Cancelling queued command {CommandId}: {Command}" , commandId , queuedCommand . Command ) ;
183-
183+
184184 try
185185 {
186186 queuedCommand . CancellationTokenSource . Cancel ( ) ;
@@ -202,7 +202,7 @@ public int CancelAllCommands(string? reason = null)
202202 {
203203 if ( m_disposed )
204204 throw new ObjectDisposedException ( nameof ( CommandQueueService ) ) ;
205-
205+
206206 var reasonText = string . IsNullOrWhiteSpace ( reason ) ? "No reason provided" : reason ;
207207 m_logger . LogWarning ( "Cancelling ALL commands. Reason: {Reason}" , reasonText ) ;
208208
@@ -266,7 +266,7 @@ public int CancelAllCommands(string? reason = null)
266266 {
267267 if ( m_disposed )
268268 throw new ObjectDisposedException ( nameof ( CommandQueueService ) ) ;
269-
269+
270270 lock ( m_currentCommandLock )
271271 {
272272 return m_currentCommand ;
@@ -277,7 +277,7 @@ public int CancelAllCommands(string? reason = null)
277277 {
278278 if ( m_disposed )
279279 throw new ObjectDisposedException ( nameof ( CommandQueueService ) ) ;
280-
280+
281281 var results = new List < ( string , string , DateTime , string ) > ( ) ;
282282
283283 // Add current command
@@ -336,7 +336,7 @@ private async Task ProcessCommandQueue()
336336 {
337337 m_currentCommand = queuedCommand ;
338338 }
339-
339+
340340 // Update command state to executing
341341 UpdateCommandState ( queuedCommand . Id , CommandState . Executing ) ;
342342
@@ -360,12 +360,12 @@ private async Task ProcessCommandQueue()
360360 var resultMessage = queuedCommand . CancellationTokenSource . Token . IsCancellationRequested
361361 ? "Command execution was cancelled."
362362 : result ;
363-
363+
364364 var wasCompleted = queuedCommand . CompletionSource . TrySetResult ( resultMessage ) ;
365-
365+
366366 // Update state atomically - this is safe even if another thread modified the command
367367 UpdateCommandState ( queuedCommand . Id , CommandState . Completed ) ;
368-
368+
369369 if ( wasCompleted )
370370 {
371371 Interlocked . Increment ( ref m_commandsProcessed ) ;
@@ -374,13 +374,13 @@ private async Task ProcessCommandQueue()
374374 {
375375 m_logger . LogDebug ( "Command {CommandId} was already completed by another thread (race condition handled)" , queuedCommand . Id ) ;
376376 }
377-
377+
378378 LogConcurrencyStats ( ) ;
379379 }
380380 catch ( OperationCanceledException )
381381 {
382382 m_logger . LogInformation ( "Command {CommandId} was cancelled during execution" , queuedCommand . Id ) ;
383-
383+
384384 // FIXED: Use TrySetResult to prevent double-completion and update state
385385 var wasCompleted = queuedCommand . CompletionSource . TrySetResult ( "Command execution was cancelled." ) ;
386386 if ( wasCompleted )
@@ -392,13 +392,13 @@ private async Task ProcessCommandQueue()
392392 {
393393 m_logger . LogDebug ( "Command {CommandId} was already completed by another thread during cancellation" , queuedCommand . Id ) ;
394394 }
395-
395+
396396 LogConcurrencyStats ( ) ;
397397 }
398398 catch ( Exception ex )
399399 {
400400 m_logger . LogError ( ex , "Error executing command {CommandId}: {Command}" , queuedCommand . Id , queuedCommand . Command ) ;
401-
401+
402402 // IMPROVED: Better error handling with detailed error information
403403 var errorMessage = ex switch
404404 {
@@ -408,10 +408,10 @@ private async Task ProcessCommandQueue()
408408 ArgumentException => $ "Invalid argument: { ex . Message } ",
409409 _ => $ "Command execution failed: { ex . GetType ( ) . Name } : { ex . Message } "
410410 } ;
411-
411+
412412 var wasCompleted = queuedCommand . CompletionSource . TrySetResult ( errorMessage ) ;
413413 UpdateCommandState ( queuedCommand . Id , CommandState . Failed ) ;
414-
414+
415415 if ( wasCompleted )
416416 {
417417 Interlocked . Increment ( ref m_commandsFailed ) ;
@@ -420,7 +420,7 @@ private async Task ProcessCommandQueue()
420420 {
421421 m_logger . LogDebug ( "Command {CommandId} was already completed by another thread during error handling" , queuedCommand . Id ) ;
422422 }
423-
423+
424424 LogConcurrencyStats ( ) ;
425425 }
426426 finally
@@ -482,14 +482,14 @@ private void LogConcurrencyStats()
482482 var failed = Interlocked . Read ( ref m_commandsFailed ) ;
483483 var cancelled = Interlocked . Read ( ref m_commandsCancelled ) ;
484484 var total = processed + failed + cancelled ;
485-
485+
486486 if ( total > 0 )
487487 {
488488 var successRate = total > 0 ? ( double ) processed / total * 100 : 0 ;
489- m_logger . LogInformation ( "Concurrency Stats - Processed: {Processed}, Failed: {Failed}, Cancelled: {Cancelled}, Success Rate: {SuccessRate:F1}%" ,
489+ m_logger . LogInformation ( "Concurrency Stats - Processed: {Processed}, Failed: {Failed}, Cancelled: {Cancelled}, Success Rate: {SuccessRate:F1}%" ,
490490 processed , failed , cancelled , successRate ) ;
491491 }
492-
492+
493493 m_lastStatsLog = now ;
494494 }
495495 }
@@ -508,8 +508,8 @@ private void CleanupCompletedCommands(object? state)
508508 // PERFORMANCE: Use ValueTuple to avoid boxing in foreach
509509 foreach ( var ( key , command ) in m_activeCommands )
510510 {
511- if ( command . State == CommandState . Completed ||
512- command . State == CommandState . Cancelled ||
511+ if ( command . State == CommandState . Completed ||
512+ command . State == CommandState . Cancelled ||
513513 command . State == CommandState . Failed )
514514 {
515515 if ( command . QueueTime < cutoffTime )
0 commit comments