Multithreading Made Easier in C#
*
*
* Multithreading Made Easier
* Created by Anand.
* Bihar,India
* User: Administrator
* Date: 4/3/2005
* Time: 2:36 PM
* See how easier its to create multithreaded
applications
* Like Java ( I have tried to imitate Java,s
Runnable and Thread )
* You have to do just 4 things :
* 1)Create a class which implements
AObject.Runnable
* 2)Create an object of above class
* 3)Create an object of AObject.Thread passing
it the reference of object in step 2
* 4) Class start() method of AObject.Thread
object
You must be thinking why I didnot try just extending System.
Threading.Thread Because Its a sealed class !!! In near future I will
be adding ThreadGroup Class to AObject Namespace and many more…
Try these codes…
mail me to and_and007in@yahoo.co.in
or anandindnr@yahoo.co.in
Enjoy !
*
* To change this template use Tools | Options |
Coding | Edit Standard Headers.
*/
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MultiThreadedWin
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button bResume2;
private System.Windows.Forms.Button bClear1;
private System.Windows.Forms.Button bResume1;
private System.Windows.Forms.Button bSuspend1;
private System.Windows.Forms.Button bSuspend2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button bThread1;
private System.Windows.Forms.Button bStop1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button bThread2;
private System.Windows.Forms.Button bStop2;
private System.Windows.Forms.Button bClear2;
private AObject.Thread tt1;
private AObject.Thread tt2;
public MainForm()
{
//
// The InitializeComponent() call is required for
Windows Forms designer support.
//
InitializeComponent();
tt1=new AObject.Thread(new PrintHello(textBox1));
tt2=new AObject.Thread(new PrintHello(textBox2));
//
// TODO: Add constructor code after the
InitializeComponent() call.
//
}
static void mess(String str)
{
System.Windows.Forms.MessageBox.Show (str);
}
[STAThread]
public static void Main(string[] args)
{
//mess("Hello");
MainForm mf;
Application.Run(mf=new MainForm());
mf.dispose ();
//mess("Bye");
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms
designer support.
/// Do not change the method contents inside the
source code editor. The Forms designer might
/// not be able to load this method if it was
changed manually.
/// </summary>
private void InitializeComponent() {
this.bClear2 = new System.Windows.Forms.Button();
this.bStop2 = new System.Windows.Forms.Button();
this.bThread2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.bStop1 = new System.Windows.Forms.Button();
this.bThread1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.bSuspend2 = new System.Windows.Forms.Button();
this.bSuspend1 = new System.Windows.Forms.Button();
this.bResume1 = new System.Windows.Forms.Button();
this.bClear1 = new System.Windows.Forms.Button();
this.bResume2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// bClear2
//
this.bClear2.Location = new
System.Drawing.Point(216, 208);
this.bClear2.Name = "bClear2";
this.bClear2.Size = new System.Drawing.Size(168,
24);
this.bClear2.TabIndex = 5;
this.bClear2.Text = "Clear";
this.bClear2.Click += new System.EventHandler(this.BClear2Click);
//
// bStop2
//
this.bStop2.Location = new
System.Drawing.Point(216, 304);
this.bStop2.Name = "bStop2";
this.bStop2.Size = new System.Drawing.Size(168,
24);
this.bStop2.TabIndex = 7;
this.bStop2.Text = "Stop Thread2";
this.bStop2.Click += new System.EventHandler(this.BStop2Click);
//
// bThread2
//
this.bThread2.Location = new
System.Drawing.Point(216, 240);
this.bThread2.Name = "bThread2";
this.bThread2.Size = new System.Drawing.Size(168,
24);
this.bThread2.TabIndex = 0;
this.bThread2.Text = "Start Thread2";
this.bThread2.Click += new System.EventHandler(this.BThread2Click);
//
// textBox2
//
this.textBox2.Location = new
System.Drawing.Point(216, 16);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox2.Size = new System.Drawing.Size(168,
184);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// bStop1
//
this.bStop1.Location = new System.Drawing.Point(16,
304);
this.bStop1.Name = "bStop1";
this.bStop1.Size = new System.Drawing.Size(168,
24);
this.bStop1.TabIndex = 6;
this.bStop1.Text = "Stop Thread1";
this.bStop1.Click += new System.EventHandler(this.BStop1Click);
//
// bThread1
//
this.bThread1.Location = new
System.Drawing.Point(16, 240);
this.bThread1.Name = "bThread1";
this.bThread1.Size = new System.Drawing.Size(168,
24);
this.bThread1.TabIndex = 1;
this.bThread1.Text = "Start Thread1";
this.bThread1.Click += new System.EventHandler(this.BThread1Click);
//
// textBox1
//
this.textBox1.Location = new
System.Drawing.Point(16, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(168,
184);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
//
// bSuspend2
//
this.bSuspend2.Location = new
System.Drawing.Point(216, 272);
this.bSuspend2.Name = "bSuspend2";
this.bSuspend2.Size = new System.Drawing.Size(168,
24);
this.bSuspend2.TabIndex = 8;
this.bSuspend2.Text = "Suspend Thread2";
this.bSuspend2.Click += new System.EventHandler(this.BSuspend2Click);
//
// bSuspend1
//
this.bSuspend1.Location = new
System.Drawing.Point(16, 272);
this.bSuspend1.Name = "bSuspend1";
this.bSuspend1.Size = new System.Drawing.Size(168,
24);
this.bSuspend1.TabIndex = 9;
this.bSuspend1.Text = "Suspend Thread1";
this.bSuspend1.Click += new System.EventHandler(this.BSuspend1Click);
//
// bResume1
//
this.bResume1.Location = new
System.Drawing.Point(16, 336);
this.bResume1.Name = "bResume1";
this.bResume1.Size = new System.Drawing.Size(168,
24);
this.bResume1.TabIndex = 11;
this.bResume1.Text = "Resume Thread1";
this.bResume1.Click += new System.EventHandler(this.BResume1Click);
//
// bClear1
//
this.bClear1.Location = new
System.Drawing.Point(16, 208);
this.bClear1.Name = "bClear1";
this.bClear1.Size = new System.Drawing.Size(168,
24);
this.bClear1.TabIndex = 4;
this.bClear1.Text = "Clear";
this.bClear1.Click += new System.EventHandler(this.BClear1Cl
ick);
/> //
// bResume2
//
this.bResume2.Location = new
System.Drawing.Point(216, 336);
this.bResume2.Name = "bResume2";
this.bResume2.Size = new System.Drawing.Size(168,
24);
this.bResume2.TabIndex = 10;
this.bResume2.Text = "Resume Thread2";
this.bResume2.Click += new System.EventHandler(this.BResume2Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5,
13);
this.ClientSize = new System.Drawing.Size(408,
365);
this.Controls.Add(this.bResume1);
this.Controls.Add(this.bResume2);
this.Controls.Add(this.bSuspend1);
this.Controls.Add(this.bSuspend2);
this.Controls.Add(this.bStop2);
this.Controls.Add(this.bStop1);
this.Controls.Add(this.bClear2);
this.Controls.Add(this.bClear1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.bThread1);
this.Controls.Add(this.bThread2);
this.Name = "MainForm";
this.Text = "MainForm";
this.Load += new
System.EventHandler(this.MainFormLoad);
this.ResumeLayout(false);
}
#endregion
void BSuspend1Click(object sender, System.EventArgs
e)
{
tt1.suspend ();
}
void BResume1Click(object sender, System.EventArgs
e)
{
tt1.resume ();
}
void BResume2Click(object sender, System.EventArgs
e)
{
tt2.resume ();
}
void BThread1Click(object sender, System.EventArgs
e)
{
tt1.start ();
}
void BClear1Click(object sender, System.EventArgs e)
{
textBox1.Text ="";
}
void BClear2Click(object sender, System.EventArgs e)
{
textBox2.Text ="";
}
void BThread2Click(object sender, System.EventArgs
e)
{
tt2.start ();
}
void BSuspend2Click(object sender, System.EventArgs
e)
{
tt2.suspend ();
}
void BStop1Click(object sender, System.EventArgs e)
{
tt1.stop ();
}
void BStop2Click(object sender, System.EventArgs e)
{
tt2.stop ();
}
void MainFormLoad(object sender, System.EventArgs e)
{
//System.Windows.Forms.MessageBox.Show ("Hello");
}
/*void MainFormClosing(object sender,
System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show ("Frnds");
}*/
void dispose()
{
//System.Windows.Forms.MessageBox.Show ("Closing");
tt1.stop ();
tt2.stop();
}
}
}
/*
Trying to create my own thread;
*/
namespace AObject
{
public class Thread
{
private System.Threading.Thread tt;
//private ThreadStart ts;
private Runnable rr;
private bool started;
private bool stopped;
private bool suspended;
public Thread(Runnable rr)
{
this.rr=rr;
started=false;
stopped=true;
}
public void start()
{
if(!started)
{
tt=new System.Threading.Thread(new System.Threading.ThreadStart(rr.run));
tt.Start ();
started=true;
stopped=false;
}
}
public void stop()
{
if (!stopped)
{
resume();
tt.Abort ();
stopped=true;
}
}
public void suspend()
{
if(!suspended&&!stopped)
{
tt.Suspend ();
suspended=true;
}
}
public void resume()
{
if(suspended&&!stopped)
{
tt.Resume();
suspended=false;
}
}
public static void sleep(int milliseconds)
{
System.Threading.Thread.Sleep(milliseconds);
}
}
public interface Runnable
{
void run();
}
}
public class HelloWorld
{
/* public static void Main()
{
}
*/
}
class PrintHello: AObject.Runnable
{
private System.Windows.Forms.TextBox textBox;
private int count;
public PrintHello(System.Windows.Forms.TextBox
textBox)
{
this.textBox =textBox;
}
public void run()
{
while(true)
{
textBox.Text =textBox.Text + "\r\n" + "Hello" +
++count;
AObject.Thread.sleep(100);
}
}
}












No comments yet... Be the first to leave a reply!