using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace CounterThreadExample
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label labelCounter;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItemStart;
private System.Windows.Forms.MenuItem menuItemPause;
private System.Windows.Forms.MenuItem menuItemResume;
public Thread OnlineDisplayThread;
public bool bThreadStarted;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
bThreadStarted=false;
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.labelCounter = new System.Windows.Forms.Label();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItemStart = new System.Windows.Forms.MenuItem();
this.menuItemPause = new System.Windows.Forms.MenuItem();
this.menuItemResume = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Count No";
//
// labelCounter
//
this.labelCounter.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.labelCounter.Location = new System.Drawing.Point(120, 40);
this.labelCounter.Name = "labelCounter";
this.labelCounter.Size = new System.Drawing.Size(64, 23);
this.labelCounter.TabIndex = 1;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItemStart,
this.menuItemPause,
this.menuItemResume});
this.menuItem1.Text = "Thread";
//
// menuItemStart
//
this.menuItemStart.Index = 0;
this.menuItemStart.Text = "Start";
this.menuItemStart.Click += new System.EventHandler(this.menuItemStart_Click);
//
// menuItemPause
//
this.menuItemPause.Index = 1;
this.menuItemPause.Text = "Pause";
this.menuItemPause.Click += new System.EventHandler(this.menuItemPause_Click);
//
// menuItemResume
//
this.menuItemResume.Index = 2;
this.menuItemResume.Text = "Resume";
this.menuItemResume.Click += new System.EventHandler(this.menuItemResume_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 189);
this.Controls.Add(this.labelCounter);
this.Controls.Add(this.label1);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItemStart_Click(object sender, System.EventArgs e)
{
OnlineDisplayThread=new Thread(new ThreadStart(ThreadCounter));
OnlineDisplayThread.Start();
bThreadStarted=true;
}
private void menuItemPause_Click(object sender, System.EventArgs e)
{
OnlineDisplayThread.Suspend();
}
private void menuItemResume_Click(object sender, System.EventArgs e)
{
OnlineDisplayThread.Resume();
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
public void ThreadCounter()
{
long iCounter=0;
try
{
while(true)
{
iCounter++;
labelCounter.Text=iCounter.ToString();
Thread.Sleep(1000);
}
}
catch(Exception ex)
{
}
}
private void Form1_Closed(object sender, System.EventArgs e)
{
try
{
if(bThreadStarted==true)
{
OnlineDisplayThread.Abort();
}
}
catch(Exception Ex)
{
}
}
}
}