private void OnTimerElapsed(object sender, ElapsedEventArgs e) { try { Execute.OnUIThread(() => { // lock to prevent multiple threads attempting to open the connection lock (connectionLockObj) { if (_connection.State != System.Data.ConnectionState.Open) { _connection.Open(); } _connection.Ping(); } }); } catch (Exception ex) { Execute.OnUIThread(() => RaiseError(ex)); //TODO stop trace and send message to reset UI } finally { // if past timeout then exit and display error if ((DateTime.UtcNow - utcPingStart).Seconds > 30) { _startingTimer.Stop(); DisposeTrace(); RaiseError("Timeout exceeded attempting to start Trace"); } } }
//Task task; //private void PingUntilTraceStarts() //{ // _pingTraceCancellationToken = new System.Threading.CancellationTokenSource(); // // cancels the ping loop after the specified number of timeout seconds from global options // _pingTraceCancellationToken.CancelAfter(new TimeSpan(0, 0, _globalOptions.TraceStartupTimeout)); // task = Task.Run(async () => // <- marked async // { // while (true) // { // PingTrace(_pingTraceCancellationToken); // await Task.Delay(10000, _pingTraceCancellationToken.Token); // <- await with cancellation // } // }, _pingTraceCancellationToken.Token); //} //private void PingTrace(System.Threading.CancellationTokenSource token) //{ // try // { // var policy = Policy // .Handle<Exception>() // .WaitAndRetry( // 3, // retryAttempt => TimeSpan.FromMilliseconds(Math.Pow(2, retryAttempt) * 100), // (exception, timeSpan, context) => // { // Log.Error(exception, "{class} {method}", "QueryTraceEngine", "PingTrace"); // System.Diagnostics.Debug.WriteLine("Error pinging trace connection: " + exception.Message); // // TODO - should we raise event aggregator // } // ); // policy.Execute(() => // { // if (_connection.State != System.Data.ConnectionState.Open) _connection.Open(); // _connection.Ping(); // Log.Verbose("{class} {method} {message}", "QueryTraceEngine", "PingTrace", "Pinging Connection"); // }); // } // catch (Exception ex) // { // Execute.OnUIThread(() => RaiseError(ex)); // //TODO stop trace and send message to reset UI // } // finally // { // // if past timeout then exit and display error // if ((DateTime.UtcNow - utcPingStart).Seconds > this.TraceStartTimeoutSecs) // { // _startingTimer.Stop(); // DisposeTrace(); // Execute.OnUIThread(() => RaiseError("Timeout exceeded attempting to start Trace")); // } // } //} private void OnTimerElapsed(object sender, ElapsedEventArgs e) { try { //Execute.OnUIThread(() => { // TODO - why did I put this on the UI thread??? // lock to prevent multiple threads attempting to open the connection lock (connectionLockObj) { var policy = Policy .Handle <Exception>() .WaitAndRetry( 3, retryAttempt => TimeSpan.FromMilliseconds(Math.Pow(2, retryAttempt) * 100), (exception, timeSpan, context) => { Log.Error(exception, "{class} {method}", "QueryTraceEngine", "OnTimerElapsed"); System.Diagnostics.Debug.WriteLine("Error pinging trace connection: " + exception.Message); // TODO - should we raise event aggregator } ); policy.Execute(() => { if (_connection.State != System.Data.ConnectionState.Open) { _connection.Open(); } _connection.Ping(); Log.Verbose("{class} {method} {message}", "QueryTraceEngine", "OnTimerElapsed", "Pinging Connection"); }); } //}); } catch (Exception ex) { Execute.OnUIThread(() => RaiseError(ex)); //TODO stop trace and send message to reset UI } finally { // if past timeout then exit and display error if ((DateTime.UtcNow - utcPingStart).Seconds > this.TraceStartTimeoutSecs) { _startingTimer.Stop(); DisposeTrace(); RaiseError("Timeout exceeded attempting to start Trace"); } } }
private void OnTimerElapsed(object sender, ElapsedEventArgs e) { Log.Debug("{class} {method} {event}", "QueryTraceEngineExcel", "OnTimerElapsed", "Ping"); //HACK - wait 1.5 seconds to allow trace to start // using the ping method thows a connection was lost error when starting a second trace Execute.OnUIThread(() => _connection.Ping()); //Execute.OnUIThread(() => ServerPing()); // if past timeout then exit and display error if ((utcPingStart - DateTime.UtcNow).Seconds > TraceStartTimeoutSecs) { _startingTimer.Stop(); _trace.Drop(); OutputError("Timeout exceeded attempting to start Trace"); Log.Warning("{class} {method} {event}", "QueryTraceEngineExcel", "OnTimerElapsed", "Timeout exceeded attempting to start Trace"); } //StopTimer(); //Status = QueryTraceStatus.Started; //_traceStarted = true; //if (TraceStarted != null) // TraceStarted(this, null); }