context: using System;
  using System.Text;
  using System.Windows.Forms;
  using System.Data.SqlClient;
  using System.Threading;
  using System.IO;
  using System.Collections;
  using System.Diagnostics;
  
  
  namespace SQLStressTool
  {
      public partial class Main : Form
      {
          #region GlobalVariables
  
          public SqlConnectionStringBuilder _SqlConnString;
          public static int _Conns;
          private int _LoopCount;
          public ManualResetEvent finished;
          public bool _Redo;
          public Thread t;
          public Thread Perf;
          public int threadCount;
          //public SqlConnectionStringBuilder _connstring;
          private bool _Stop = false;
          
          public StringBuilder _ExecutionLog;
          public ArrayList _PerformanceResutls;
  
          public string[] _Selectfiles;
          public string[] _files;
          public bool _KeepRunning = true;
          private int _MinsToRun = 0;
          private int _MaxFiles = 30;
          private int _MaxConns = 150;
          private DateTime StartTime;
          private DateTime _Crono;
          
          public StreamWriter _PerfLog;
          //public PerformanceCounter _Mem1 = null;
          //public PerformanceCounter _Mem2 = null;
          //public PerformanceCounter _Proc1 = null;
          //public PerformanceCounter[] _Proc2 = null;
          //public PerformanceCounter[] _Disk1 = null;
          //public PerformanceCounter[] _Disk2 = null;
          //public PerformanceCounter[] _Disk3 = null;
          //bool _roundwait = true;
          //string _PerfPath = Directory.GetCurrentDirectory();
  
          #endregion
          public Main()
          {
              InitializeComponent();
          }
          void Main_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
          {
              if (e.Control && e.KeyCode == Keys.N)
              {
                  if (!New_Panel.Visible)
                      newTestToolStripMenuItem_Click(null, null);
              }
              if (e.Control && e.KeyCode == Keys.V)
                  e.Handled = true;
          }
  
          private void NewTest_Click(object sender, EventArgs e)
          {
              if (!New_Panel.Visible)
              {
                  NewTest.Visible = false;
                  NewTest.Enabled = false;
                  newTestToolStripMenuItem_Click(null, null);
              }
          }
          private void CloseTestButton_Click_1(object sender, EventArgs e)
          {
              btn_Stop_Click(null, null);
              closetestmenu_Click(null, null);
          }
  
  
          #region Menu
  
          private void newTestToolStripMenuItem_Click(object sender, EventArgs e)
          {
              closetestmenu.Enabled = true;
              newTestToolStripMenuItem.Enabled = false;
              New_Panel.Enabled = true;
              New_Panel.Visible = true;
              InitialValues();
          }
          private void closetestmenu_Click(object sender, EventArgs e)
          {
              closetestmenu.Enabled = false;
              newTestToolStripMenuItem.Enabled = true;
              New_Panel.Enabled = false;
              New_Panel.Visible = false;
              NewTest.Visible = true;
              NewTest.Enabled = true;
              InitialValues();
          }
          private void exitToolStripMenuItem_Click(object sender, EventArgs e)
          {
              this.Dispose();
              this.Close();
          }
  
          private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
          {
              About _about = new About();
              _about.ShowDialog();
          }
  
          #endregion
  
          #region ConfigureStress
  
          void txt_Connections_TextChanged(object sender, EventArgs e)
          {
              if ((txt_Connections.TextLength > 0) && (Convert.ToInt32(txt_Connections.Text) <= _MaxConns))
              {
                  if (CheckInfo())
                      btn_Start.Enabled = true;
                  else
                      btn_Start.Enabled = false;
              }
              else if (txt_Connections.TextLength > 0)
              {
                  MessageBox.Show("The maximum value cannot exceed " + _MaxConns.ToString() + ".", "Concurrent Connections", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  txt_Connections.Text = "25";
              }
          }
          void txt_Connections_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
          {
              if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                  e.Handled = true;
          }
  
          void DurationMinutes_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
          {
              if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                  e.Handled = true;
          }
          void DurationMinutes_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
          {
              if (DurationMinutes.Value > 2880)
                  DurationMinutes.Value = 2880;
          }
  
          void textBox1_TextChanged(object sender, System.EventArgs e)
          {
              if (CheckInfo())
                  btn_Start.Enabled = true;
              else
                  btn_Start.Enabled = false;
          }
  
          void txt_progress_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
          {
              if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                  e.Handled = true;
          }
  
          void txt_progress_TextChanged(object sender, System.EventArgs e)
          {
              if (txt_progress.Text.Length > 0)
              {
                  if (Convert.ToInt32(txt_progress.Text) > 100)
                  {
                      MessageBox.Show("The Porcentage can't be bigger than 100. Please correct the value", "Percentage Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                      txt_progress.Text = string.Empty;
                  }
              }
              if (CheckInfo())
                  btn_Start.Enabled = true;
              else
                  btn_Start.Enabled = false;
          }
  
          private void chk_progress_CheckedChanged(object sender, EventArgs e)
          {
              if (chk_progress.Checked == true)
              {
                  txt_progress.Text = string.Empty;
                  txt_progress.Enabled = true;
              }
              else
              {
                  txt_progress.Text = "100";
                  txt_progress.Enabled = false;
              }
          }
  
          private void chk_continous_CheckedChanged(object sender, EventArgs e)
          {
              if (chk_continous.Checked == true)
              {
                  txt_progress.Enabled = false;
                  txt_Connections.Enabled = false;
                  chk_progress.Enabled = false;
                  DurationMinutes.Enabled = false;
  
                  txt_progress.Text = "100";
                  txt_Connections.Text = "1";
              }
              else
              {
                  txt_progress.Enabled = true;
                  txt_Connections.Enabled = true;
                  chk_progress.Enabled = true;
                  DurationMinutes.Enabled = true;
              }
          }
  
          void txt_selectFiles_TextChanged(object sender, System.EventArgs e)
          {
              if (CheckInfo())
                  btn_Start.Enabled = true;
              else
                  btn_Start.Enabled = false;
          }
  
          private void btn_browseSelect_Click(object sender, EventArgs e)
          {
              //int file = 0;
              DialogResult _FolderSelect = this.BrowseFolder.ShowDialog();
              if (_FolderSelect == DialogResult.OK)
              {
                  WarningLabel.Visible = false;
                  txt_DirSelect.Text = this.BrowseFolder.SelectedPath.ToString();
  
                  //file = Directory.GetFiles(txt_DirSelect.Text, "*.sql").Length;
  
                  txt_selectFiles.Text = string.Empty;
  
                  _files = Directory.GetFiles(txt_DirSelect.Text, "*.sql");
  
                  //foreach (string _file in _files)
                  //{
                  //    txt_selectFiles.Text += _file +"\r\n";
                  //    file++;
                  //    if (file == _MaxFiles)
                  //    {
                  //        WarningLabel.Text = "The folder contains more files than the allowed. Only the first 30 will be used.";
                  //        WarningLabel.Visible = true;
                  //        break;
                  //    }
                  //}
              }
              txt_selectFiles.Text = _files.Length.ToString();
          }
  
  
          bool CheckInfo()
          {
              bool cons = false;
              bool srv = false;
              bool files = false;
              bool progress = false;
  
              if ((txt_Connections.TextLength > 0) && (Convert.ToInt32(txt_Connections.Text) >= 1))
                  cons = true;
  
              if (textBox1.TextLength > 0)
                  srv = true;
  
              if (txt_selectFiles.Text.Trim().Length > 0)
                  files = true;
  
              if (txt_progress.Text.Trim().Length > 0)
                  progress = true;
  
              if (cons && srv && files && progress)
                  return true;
              else
                  return false;
          }
  
          #endregion
  
          #region StartStress
  
          public void btn_Start_Click(object sender, EventArgs e)
          {
              
              try
              {
                  int files = Directory.GetFiles(txt_DirSelect.Text, "*.sql").Length;
  
  
                  closetestmenu.Enabled = false;
                  _Crono = Convert.ToDateTime("00:00:00");
                  StartTime = DateTime.Now;
                  ElapsedTimeOfTest.Enabled = true;
                  _KeepRunning = true;
                  CleanUpStuff();
                  DisableControls();
                  _ExecutionLog = new StringBuilder();
                  _ExecutionLog.AppendLine(string.Concat(DateTime.Now.ToUniversalTime().ToString(), " - Start of load test"));
                  txt_results.BeginInvoke(new MethodInvoker(delegate () { txt_results.Text += "Start of load test.\r\n"; }));
                  _Stop = false;
                  bool _filevalid = true;
                  //string[] _Selectfiles = txt_selectFiles.Lines;
                  string[] _Selectfiles = _files;
                  string _filestoprocess = string.Empty;
                  object _Objs;
  
                  foreach (string _file in _Selectfiles)
                  {
                      if (_file.Length > 0)
                      {
                          if (!File.Exists(_file))
                          {
                              _filevalid = false;
                              MessageBox.Show(string.Concat("File ", _file, " does not exist."), "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                              _ExecutionLog.AppendLine(string.Concat(DateTime.Now.ToUniversalTime().ToString(), " - File ", _file, " does not exist."));
                          }
  
gt:                         if (_filestoprocess == string.Empty)