@@ -36,7 +36,8 @@ public interface ISimulationContext
3636 /// </summary>
3737 /// <param name="simulationId">The simulation ID.</param>
3838 /// <param name="simulationType">The type of simulation.</param>
39- void TrackSimulationStarted ( Guid simulationId , string simulationType ) ;
39+ /// <param name="waitForTransmission">If true, blocks until telemetry transmission completes. Use for CPU-intensive simulations.</param>
40+ void TrackSimulationStarted ( Guid simulationId , string simulationType , bool waitForTransmission = false ) ;
4041
4142 /// <summary>
4243 /// Tracks a SimulationEnded event in Application Insights.
@@ -101,9 +102,9 @@ public IDisposable SetContext(Guid simulationId, string simulationType, bool tra
101102 }
102103
103104 /// <inheritdoc />
104- public void TrackSimulationStarted ( Guid simulationId , string simulationType )
105+ public void TrackSimulationStarted ( Guid simulationId , string simulationType , bool waitForTransmission = false )
105106 {
106- TrackSimulationEvent ( "SimulationStarted" , simulationId , simulationType ) ;
107+ TrackSimulationEvent ( "SimulationStarted" , simulationId , simulationType , waitForTransmission ) ;
107108 }
108109
109110 /// <inheritdoc />
@@ -115,7 +116,11 @@ public void TrackSimulationEnded(Guid simulationId, string simulationType)
115116 /// <summary>
116117 /// Tracks a simulation event in Application Insights.
117118 /// </summary>
118- internal void TrackSimulationEvent ( string eventName , Guid simulationId , string simulationType )
119+ /// <param name="eventName">The event name (SimulationStarted/SimulationEnded).</param>
120+ /// <param name="simulationId">The simulation ID.</param>
121+ /// <param name="simulationType">The simulation type.</param>
122+ /// <param name="waitForTransmission">If true, blocks until transmission completes (use for CPU-intensive simulations).</param>
123+ internal void TrackSimulationEvent ( string eventName , Guid simulationId , string simulationType , bool waitForTransmission = false )
119124 {
120125 _logger . LogInformation (
121126 "Tracking App Insights event: {EventName} for simulation {SimulationId} ({SimulationType})" ,
@@ -128,7 +133,7 @@ internal void TrackSimulationEvent(string eventName, Guid simulationId, string s
128133
129134 if ( telemetryClient == null )
130135 {
131- _logger . LogDebug ( "TelemetryClient not available (App Insights not configured), skipping event tracking" ) ;
136+ _logger . LogWarning ( "TelemetryClient not available (App Insights not configured), skipping event tracking" ) ;
132137 return ;
133138 }
134139
@@ -140,9 +145,17 @@ internal void TrackSimulationEvent(string eventName, Guid simulationId, string s
140145
141146 telemetryClient . TrackEvent ( eventName , properties ) ;
142147
143- // Flush to ensure event is sent immediately (important for short-lived simulations)
148+ // Flush to ensure event is sent
144149 telemetryClient . Flush ( ) ;
145150
151+ // For CPU-intensive operations, wait to ensure transmission completes
152+ // before background threads saturate all cores
153+ if ( waitForTransmission )
154+ {
155+ _logger . LogDebug ( "Waiting for telemetry transmission to complete..." ) ;
156+ Thread . Sleep ( 1000 ) ; // Give network I/O time to complete
157+ }
158+
146159 _logger . LogDebug ( "Successfully tracked and flushed event {EventName}" , eventName ) ;
147160 }
148161 catch ( Exception ex )
0 commit comments