public async Task Run() { ConsoleWrite.Line(); ConsoleWrite.Line($"Starting {this.GetType().Name}.{nameof(Run)}."); string srcName = DiagnosticEventsSpecification.DirectSourceName; DiagnosticSource diagnosticSource = new DiagnosticListener(srcName); Random rnd = new Random(); int currIteration = CurrentIteration; while (currIteration < _maxInterations) { if (diagnosticSource.IsEnabled(DiagnosticEventsSpecification.DirectSourceEventName)) { diagnosticSource.Write(DiagnosticEventsSpecification.DirectSourceEventName, new DiagnosticEventsSpecification.EventPayload(currIteration, srcName)); } if (currIteration == _phaseOneIterations) { PhaseOneCompletedEvent.Set(); } int sleepMillis = rnd.Next(MaxSleepMillis); if (sleepMillis == 0) { ; } if (sleepMillis == 1) { Thread.Yield(); } else { await Task.Delay(sleepMillis); } currIteration = Interlocked.Increment(ref _currentIteration); } ConsoleWrite.Line(); ConsoleWrite.Line($"Finishing {this.GetType().Name}.{nameof(Run)}."); if (diagnosticSource is IDisposable disposableSource) { disposableSource.Dispose(); } ConsoleWrite.Line($"Finished {this.GetType().Name}.{nameof(Run)}."); }
public async Task Run() { ConsoleWrite.LineLine($"Starting {this.GetType().Name}.{nameof(Run)}."); Random rnd = new Random(); DiagnosticSourceAssembly.SubscribeDynamicInvokerInitializedListener(OnDynamicDiagnosticSourceInvokerInitialized, rnd); { ConsoleWrite.LineLine($"Kicking off the DS magic."); bool prevInit = DiagnosticSourceAssembly.IsInitialized; bool nowInit = DiagnosticSourceAssembly.EnsureInitialized(); ConsoleWrite.Line($"DiagnosticSourceAssembly-magic status: prevInit={prevInit}, nowInit={nowInit}."); } int currIteration = CurrentIteration; while (currIteration < _maxInterations) { WriteIfEnabled(_diagnosticSource, currIteration); if (currIteration == _phaseOneIterations) { PhaseOneCompletedEvent.Set(); } int sleepMillis = rnd.Next(MaxSleepMillis); if (sleepMillis == 0) { ; } else if (sleepMillis == 1) { Thread.Yield(); } else { await Task.Delay(sleepMillis - 2); } currIteration = Interlocked.Increment(ref _currentIteration); } ConsoleWrite.Line(); ConsoleWrite.Line($"Finishing {this.GetType().Name}.{nameof(Run)}."); Dispose(_diagnosticSource); ConsoleWrite.Line($"Finished {this.GetType().Name}.{nameof(Run)}."); }