//
// Copyright (c) 2002
// Ronald Kevin Burton
//
// Z poniższym kodem nie jest związana żadna gwarancja poprawności działania.
// Program został dołączony do książki ".NET CLR. Księga eksperta" w celu
// ilustracji koncepcji i zasad przedstawionych w tej książce. Program może być 
// używany na własne ryzyko.
//
// Przyznaje się prawo do użycia lub kopiowania tego oprogramowania do dowolnego celu
// bez konieczności ponoszenia żadnych opłat pod warunkiem, że powyższe uwagi zostaną 
// zachowane we wszystkich kopiach. Przyznaje się także prawo do modyfikacji kodu
// i dystrybucji zmodyfikowanego kodu pod warunkiem zachowania powyższych uwag
// oraz dołączenia informacji mówiącej o modyfikacji kodu.
//
// 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Benchmark
{
	/// <summary>
	/// Podsumowanie dla BenchmarkForm.
	/// </summary>
	public class BenchmarkForm : System.Windows.Forms.Form
	{ 
		private System.Windows.Forms.TextBox threadThreadCount;
		private System.Windows.Forms.TextBox threadIterations;
		private System.Windows.Forms.Button threadButton;
		private System.Windows.Forms.Label numberOfThreadsLabel;
		private System.Windows.Forms.Label numberOfCyclesLabel;
		private System.Windows.Forms.Label resultsLabel;
		private System.Windows.Forms.TextBox threadPoolThreadCount;
		private System.Windows.Forms.Button threadPoolButton;
		private System.Windows.Forms.TextBox threadAverageElapsed;
		private System.Windows.Forms.TextBox threadPoolAverageElapsed;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox threadAverageStartElapsed;
		private System.Windows.Forms.TextBox threadAverageStopElapsed;
		private System.Windows.Forms.TextBox threadPoolAverageStartElapsed;
		private System.Windows.Forms.TextBox threadPoolAverageStopElapsed;
		private System.Windows.Forms.TextBox threadPoolIterations;
		private System.Windows.Forms.TrackBar workBar;
		private System.Windows.Forms.TextBox workValue;
		private System.Windows.Forms.Label workLabel;
		/// <summary>
		/// Wymagana zmienna.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public BenchmarkForm()
		{
			//
			// Wymagane do obsługi Windows Form Designer
			//
			InitializeComponent();

			//
			// TODO: Dodanie kodu konstruktora po wywołaniu InitializeComponent
			//
		}

		/// <summary>
		/// Oczyszczenie używanych zasobów.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Metoda wymagana do obsługi Designera - nie należy modyfikować
		/// zawartości tej metody przy użyciu edytora kodu.
		/// </summary>
		private void InitializeComponent()
		{
			this.threadIterations = new System.Windows.Forms.TextBox();
			this.threadPoolButton = new System.Windows.Forms.Button();
			this.numberOfThreadsLabel = new System.Windows.Forms.Label();
			this.workBar = new System.Windows.Forms.TrackBar();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.threadAverageStopElapsed = new System.Windows.Forms.TextBox();
			this.threadPoolIterations = new System.Windows.Forms.TextBox();
			this.threadButton = new System.Windows.Forms.Button();
			this.threadAverageStartElapsed = new System.Windows.Forms.TextBox();
			this.workValue = new System.Windows.Forms.TextBox();
			this.threadPoolAverageStartElapsed = new System.Windows.Forms.TextBox();
			this.threadAverageElapsed = new System.Windows.Forms.TextBox();
			this.threadPoolThreadCount = new System.Windows.Forms.TextBox();
			this.workLabel = new System.Windows.Forms.Label();
			this.numberOfCyclesLabel = new System.Windows.Forms.Label();
			this.threadPoolAverageElapsed = new System.Windows.Forms.TextBox();
			this.threadThreadCount = new System.Windows.Forms.TextBox();
			this.resultsLabel = new System.Windows.Forms.Label();
			this.threadPoolAverageStopElapsed = new System.Windows.Forms.TextBox();
			((System.ComponentModel.ISupportInitialize)(this.workBar)).BeginInit();
			this.SuspendLayout();
			// 
			// threadIterations
			// 
			this.threadIterations.Location = new System.Drawing.Point(144, 56);
			this.threadIterations.Name = "threadIterations";
			this.threadIterations.TabIndex = 1;
			this.threadIterations.Text = "10";
			// 
			// threadPoolButton
			// 
			this.threadPoolButton.Location = new System.Drawing.Point(304, 88);
			this.threadPoolButton.Name = "threadPoolButton";
			this.threadPoolButton.TabIndex = 10;
			this.threadPoolButton.Text = "Pula wątków";
			this.threadPoolButton.Click += new System.EventHandler(this.OnThreadPool);
			// 
			// numberOfThreadsLabel
			// 
			this.numberOfThreadsLabel.Location = new System.Drawing.Point(0, 24);
			this.numberOfThreadsLabel.Name = "numberOfThreadsLabel";
			this.numberOfThreadsLabel.Size = new System.Drawing.Size(104, 23);
			this.numberOfThreadsLabel.TabIndex = 5;
			this.numberOfThreadsLabel.Text = "Liczba wątków";
			// 
			// workBar
			// 
			this.workBar.Location = new System.Drawing.Point(136, 216);
			this.workBar.Maximum = 40;
			this.workBar.Name = "workBar";
			this.workBar.Size = new System.Drawing.Size(264, 42);
			this.workBar.TabIndex = 12;
			this.workBar.TickFrequency = 2;
			this.workBar.Scroll += new System.EventHandler(this.OnScroll);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(0, 152);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(120, 24);
			this.label1.TabIndex = 7;
			this.label1.Text = "Średni czas uruchomienia";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(0, 184);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(120, 24);
			this.label2.TabIndex = 7;
			this.label2.Text = "Średni czas zatrzymania";
			// 
			// threadAverageStopElapsed
			// 
			this.threadAverageStopElapsed.Location = new System.Drawing.Point(144, 184);
			this.threadAverageStopElapsed.Name = "threadAverageStopElapsed";
			this.threadAverageStopElapsed.ReadOnly = true;
			this.threadAverageStopElapsed.TabIndex = 3;
			this.threadAverageStopElapsed.Text = "";
			// 
			// threadPoolIterations
			// 
			this.threadPoolIterations.Location = new System.Drawing.Point(296, 56);
			this.threadPoolIterations.Name = "threadPoolIterations";
			this.threadPoolIterations.TabIndex = 9;
			this.threadPoolIterations.Text = "10";
			// 
			// threadButton
			// 
			this.threadButton.Location = new System.Drawing.Point(160, 88);
			this.threadButton.Name = "threadButton";
			this.threadButton.TabIndex = 4;
			this.threadButton.Text = "Wątek";
			this.threadButton.Click += new System.EventHandler(this.OnThread);
			// 
			// threadAverageStartElapsed
			// 
			this.threadAverageStartElapsed.Location = new System.Drawing.Point(144, 152);
			this.threadAverageStartElapsed.Name = "threadAverageStartElapsed";
			this.threadAverageStartElapsed.ReadOnly = true;
			this.threadAverageStartElapsed.TabIndex = 3;
			this.threadAverageStartElapsed.Text = "";
			// 
			// workValue
			// 
			this.workValue.Location = new System.Drawing.Point(232, 264);
			this.workValue.Name = "workValue";
			this.workValue.ReadOnly = true;
			this.workValue.Size = new System.Drawing.Size(64, 20);
			this.workValue.TabIndex = 13;
			this.workValue.Text = "";
			this.workValue.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// threadPoolAverageStartElapsed
			// 
			this.threadPoolAverageStartElapsed.Location = new System.Drawing.Point(296, 152);
			this.threadPoolAverageStartElapsed.Name = "threadPoolAverageStartElapsed";
			this.threadPoolAverageStartElapsed.ReadOnly = true;
			this.threadPoolAverageStartElapsed.TabIndex = 11;
			this.threadPoolAverageStartElapsed.Text = "";
			// 
			// threadAverageElapsed
			// 
			this.threadAverageElapsed.Location = new System.Drawing.Point(144, 120);
			this.threadAverageElapsed.Name = "threadAverageElapsed";
			this.threadAverageElapsed.ReadOnly = true;
			this.threadAverageElapsed.TabIndex = 3;
			this.threadAverageElapsed.Text = "";
			// 
			// threadPoolThreadCount
			// 
			this.threadPoolThreadCount.Location = new System.Drawing.Point(296, 24);
			this.threadPoolThreadCount.Name = "threadPoolThreadCount";
			this.threadPoolThreadCount.TabIndex = 8;
			this.threadPoolThreadCount.Text = "10";
			// 
			// workLabel
			// 
			this.workLabel.Location = new System.Drawing.Point(8, 224);
			this.workLabel.Name = "workLabel";
			this.workLabel.Size = new System.Drawing.Size(120, 16);
			this.workLabel.TabIndex = 7;
			this.workLabel.Text = "Działaj";
			// 
			// numberOfCyclesLabel
			// 
			this.numberOfCyclesLabel.Location = new System.Drawing.Point(0, 56);
			this.numberOfCyclesLabel.Name = "numberOfCyclesLabel";
			this.numberOfCyclesLabel.TabIndex = 6;
			this.numberOfCyclesLabel.Text = "Liczba cykli";
			// 
			// threadPoolAverageElapsed
			// 
			this.threadPoolAverageElapsed.Location = new System.Drawing.Point(296, 120);
			this.threadPoolAverageElapsed.Name = "threadPoolAverageElapsed";
			this.threadPoolAverageElapsed.ReadOnly = true;
			this.threadPoolAverageElapsed.TabIndex = 11;
			this.threadPoolAverageElapsed.Text = "";
			// 
			// threadThreadCount
			// 
			this.threadThreadCount.Location = new System.Drawing.Point(144, 24);
			this.threadThreadCount.Name = "threadThreadCount";
			this.threadThreadCount.TabIndex = 0;
			this.threadThreadCount.Text = "10";
			// 
			// resultsLabel
			// 
			this.resultsLabel.Location = new System.Drawing.Point(0, 120);
			this.resultsLabel.Name = "resultsLabel";
			this.resultsLabel.Size = new System.Drawing.Size(120, 24);
			this.resultsLabel.TabIndex = 7;
			this.resultsLabel.Text = "Średni czas pracy";
			// 
			// threadPoolAverageStopElapsed
			// 
			this.threadPoolAverageStopElapsed.Location = new System.Drawing.Point(296, 184);
			this.threadPoolAverageStopElapsed.Name = "threadPoolAverageStopElapsed";
			this.threadPoolAverageStopElapsed.ReadOnly = true;
			this.threadPoolAverageStopElapsed.TabIndex = 11;
			this.threadPoolAverageStopElapsed.Text = "";
			// 
			// BenchmarkForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(448, 293);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.workLabel,
																		  this.workValue,
																		  this.workBar,
																		  this.threadPoolAverageStopElapsed,
																		  this.threadPoolAverageStartElapsed,
																		  this.threadAverageStopElapsed,
																		  this.threadAverageStartElapsed,
																		  this.label2,
																		  this.label1,
																		  this.threadPoolAverageElapsed,
																		  this.threadPoolButton,
																		  this.threadPoolIterations,
																		  this.threadPoolThreadCount,
																		  this.resultsLabel,
																		  this.numberOfCyclesLabel,
																		  this.numberOfThreadsLabel,
																		  this.threadButton,
																		  this.threadAverageElapsed,
																		  this.threadIterations,
																		  this.threadThreadCount});
			this.Name = "BenchmarkForm";
			this.Text = "Wydajność puli wątków";
			((System.ComponentModel.ISupportInitialize)(this.workBar)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Główny punkt wejścia dla aplikacji.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new BenchmarkForm());
		}

		private void OnThread(object sender, System.EventArgs e)
		{
			threadAverageElapsed.Clear();
			threadAverageElapsed.Refresh();
			threadAverageStartElapsed.Clear();
			threadAverageStartElapsed.Refresh();
			threadAverageStopElapsed.Clear();
			threadAverageStopElapsed.Refresh();

			ThreadTest tt = new ThreadTest(Convert.ToInt32(threadThreadCount.Text), Convert.ToInt32(threadIterations.Text), workBar.Value);
			tt.Start();

			threadAverageElapsed.Text = Convert.ToString(tt.AverageTotalElapsed);
			threadAverageStartElapsed.Text = Convert.ToString(tt.AverageStartElapsed);
			threadAverageStopElapsed.Text = Convert.ToString(tt.AverageStopElapsed);
		}

		private void OnThreadPool(object sender, System.EventArgs e)
		{
			threadPoolAverageElapsed.Clear();
			threadPoolAverageElapsed.Refresh();
			threadPoolAverageStartElapsed.Clear();
			threadPoolAverageStartElapsed.Refresh();
			threadPoolAverageStopElapsed.Clear();
			threadPoolAverageStopElapsed.Refresh();

			ThreadPoolTest tp = new ThreadPoolTest(Convert.ToInt32(threadPoolThreadCount.Text), Convert.ToInt32(threadPoolIterations.Text), workBar.Value);
			tp.Start();

			threadPoolAverageElapsed.Text = Convert.ToString(tp.AverageTotalElapsed);
			threadPoolAverageStartElapsed.Text = Convert.ToString(tp.AverageStartElapsed);
			threadPoolAverageStopElapsed.Text = Convert.ToString(tp.AverageStopElapsed);
		}

		private void OnScroll(object sender, System.EventArgs e)
		{
			workValue.Text = Convert.ToString(workBar.Value);
		}
	}
}
