public void startLoadThread() { try { //do the work using (SqlConnection conn = query_comm.Connection) { int connectionHashCode = conn.GetHashCode(); SqlInfoMessageEventHandler handler = new SqlInfoMessageEventHandler(queryInput.GetInfoMessages); for (int i = 0; i < iterations; i++) { if (runCancelled) { throw new Exception(); } Exception outException = null; try { //initialize the outInfo structure queryInput.outInfo = new queryOutput(); conn.Open(); //set up the statistics gathering if (stats_comm != null) { stats_comm.ExecuteNonQuery(); Thread.Sleep(0); conn.InfoMessage += handler; } //Params are assigned only once -- after that, their values are dynamically retrieved if (query_comm.Parameters.Count > 0) { ParamServer.GetNextRow_Values(query_comm.Parameters); } sw.Start(); if (forceDataRetrieval) { SqlDataReader reader = query_comm.ExecuteReader(); Thread.Sleep(0); do { Thread.Sleep(0); while (reader.Read()) { //grab the first column to force the row down the pipe object x = reader[0]; Thread.Sleep(0); } } while (reader.NextResult()); } else { query_comm.ExecuteNonQuery(); Thread.Sleep(0); } sw.Stop(); } catch (Exception e) { if (runCancelled) { throw; } else { outException = e; } if (sw.IsRunning) { sw.Stop(); } } finally { //Clean up the connection if (stats_comm != null) { conn.InfoMessage -= handler; } conn.Close(); } bool finished = (i == (iterations - 1)) ? true : false; //List<string> infoMessages = null; //infoMessages = (stats_comm != null) ? theInfoMessages[connectionHashCode] : null; /* * queryOutput theout = new queryOutput( * outException, * sw.Elapsed, * finished, * (infoMessages == null || infoMessages.Count == 0) ? null : infoMessages.ToArray()); */ outInfo.e = outException; outInfo.time = sw.Elapsed; outInfo.finished = finished; lock (LoadEngine.queryOutInfo) { LoadEngine.queryOutInfo.Enqueue(outInfo); Monitor.Pulse(LoadEngine.queryOutInfo); } //Prep the collection for the next round //if (infoMessages != null && infoMessages.Count > 0) // infoMessages.Clear(); sw.Reset(); } } } catch { if (runCancelled) { //queryOutput theout = new queryOutput(null, new TimeSpan(0), true, null); outInfo.time = new TimeSpan(0); outInfo.finished = true; lock (LoadEngine.queryOutInfo) { LoadEngine.queryOutInfo.Enqueue(outInfo); } } else { throw; } } }
public void StartLoadThread() { try { //do the work using (var conn = _queryComm.Connection) { SqlInfoMessageEventHandler handler = GetInfoMessages; for (var i = 0; i < _iterations; i++) { if (_runCancelled) { throw new Exception(); } Exception outException = null; try { //initialize the outInfo structure _outInfo = new QueryOutput(); conn.Open(); //set up the statistics gathering if (_statsComm != null) { _statsComm.ExecuteNonQuery(); Thread.Sleep(0); conn.InfoMessage += handler; } //Params are assigned only once -- after that, their values are dynamically retrieved if (_queryComm.Parameters.Count > 0) { ParamServer.GetNextRow_Values(_queryComm.Parameters); } _sw.Start(); //TODO: This could be made better if (_forceDataRetrieval) { var reader = _queryComm.ExecuteReader(); Thread.Sleep(0); do { Thread.Sleep(0); while (reader.Read()) { //grab the first column to force the row down the pipe var x = reader[0]; Thread.Sleep(0); } } while (reader.NextResult()); } else { _queryComm.ExecuteNonQuery(); Thread.Sleep(0); } _sw.Stop(); } catch (Exception e) { if (_runCancelled) { throw; } else { outException = e; } if (_sw.IsRunning) { _sw.Stop(); } } finally { //Clean up the connection if (_statsComm != null) { conn.InfoMessage -= handler; } conn.Close(); } var finished = i == _iterations - 1; //List<string> infoMessages = null; //infoMessages = (stats_comm != null) ? theInfoMessages[connectionHashCode] : null; /* * queryOutput theout = new queryOutput( * outException, * sw.Elapsed, * finished, * (infoMessages == null || infoMessages.Count == 0) ? null : infoMessages.ToArray()); */ _outInfo.E = outException; _outInfo.Time = _sw.Elapsed; _outInfo.Finished = finished; lock (QueryOutInfo) { QueryOutInfo.Enqueue(_outInfo); Monitor.Pulse(QueryOutInfo); } //Prep the collection for the next round //if (infoMessages != null && infoMessages.Count > 0) // infoMessages.Clear(); _sw.Reset(); } } } catch { if (_runCancelled) { //queryOutput theout = new queryOutput(null, new TimeSpan(0), true, null); _outInfo.Time = new TimeSpan(0); _outInfo.Finished = true; lock (QueryOutInfo) { QueryOutInfo.Enqueue(_outInfo); } } else { throw; } } }
public void StartLoadThread(Object token) { bool runCancelled = false; CancellationToken ctsToken = (CancellationToken)token; try { ctsToken.Register(() => { // Cancellation on the token will interrupt and cancel the thread runCancelled = true; _statsComm.Cancel(); _queryComm.Cancel(); }); //do the work using (var conn = _queryComm.Connection) { SqlInfoMessageEventHandler handler = GetInfoMessages; for (var i = 0; i < _iterations && !runCancelled; i++) { Exception outException = null; try { //initialize the outInfo structure _outInfo = new QueryOutput(); if (conn != null) { conn.Open(); //set up the statistics gathering if (_statsComm != null) { _statsComm.ExecuteNonQuery(); Thread.Sleep(0); conn.InfoMessage += handler; } } //Params are assigned only once -- after that, their values are dynamically retrieved if (_queryComm.Parameters.Count > 0) { ParamServer.GetNextRow_Values(_queryComm.Parameters); } _sw.Start(); //TODO: This could be made better if (_forceDataRetrieval) { var reader = _queryComm.ExecuteReader(); Thread.Sleep(0); do { Thread.Sleep(0); while (!runCancelled && reader.Read()) { //grab the first column to force the row down the pipe // ReSharper disable once UnusedVariable var x = reader[0]; Thread.Sleep(0); } } while (!runCancelled && reader.NextResult()); } else { _queryComm.ExecuteNonQuery(); Thread.Sleep(0); } _sw.Stop(); } catch (Exception ex) { if (!runCancelled) { outException = ex; } if (_sw.IsRunning) { _sw.Stop(); } } finally { //Clean up the connection if (conn != null) { if (_statsComm != null) { conn.InfoMessage -= handler; } conn.Close(); } } var finished = i == _iterations - 1; //List<string> infoMessages = null; //infoMessages = (stats_comm != null) ? theInfoMessages[connectionHashCode] : null; /* * queryOutput theout = new queryOutput( * outException, * sw.Elapsed, * finished, * (infoMessages == null || infoMessages.Count == 0) ? null : infoMessages.ToArray()); */ _outInfo.E = outException; _outInfo.Time = _sw.Elapsed; _outInfo.Finished = finished; QueryOutInfo.Add(_outInfo); //Prep the collection for the next round //if (infoMessages != null && infoMessages.Count > 0) // infoMessages.Clear(); _sw.Reset(); if (!runCancelled) { try { if (_queryDelay > 0) { Task.Delay(_queryDelay, ctsToken).Wait(); } } catch (AggregateException ae) { ae.Handle((x) => { if (x is TaskCanceledException) { runCancelled = true; return(true); } // if we get here, the exception wasn't a cancel // so don't swallow it return(false); }); } } } } } catch (Exception) { if (runCancelled) { //queryOutput theout = new queryOutput(null, new TimeSpan(0), true, null); _outInfo.Time = new TimeSpan(0); _outInfo.Finished = true; QueryOutInfo.Add(_outInfo); } } Interlocked.Increment(ref _finishedThreads); if (_finishedThreads == _numWorkerThreads) { // once all of the threads have exited, tell the other side that we're done adding items to the collection QueryOutInfo.CompleteAdding(); } }