public partial class Form1 : Form
{
delegate void SetTextCallback(string text);
Thread t1 = null;
Thread t2 = null;
Thread t3 = null;
public Form1()
{
InitializeComponent();
}
private void AddText(string text)
{
if (this.listBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(AddText);
this.Invoke(d, new object[] { text });
}
else
{
this.listBox1.Items.Add(text);
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
}
}
private void T1()
{
for (int i = 0; i < 100; i++)
{
AddText("Thread 1 : " + i.ToString());
Thread.Sleep(1800);
}
}
private void T2()
{
for (int i = 0; i < 100; i++)
{
AddText("Thread 2 : " + i.ToString());
Thread.Sleep(1000);
}
}
private void T3()
{
for (int i = 0; i < 100; i++)
{
AddText("Thread 3 : " + i.ToString());
Thread.Sleep(1500);
}
}
private void t1sr_Click(object sender, EventArgs e)
{
if (t1 == null t1.ThreadState == ThreadState.Stopped)
{
t1 = new Thread(new ThreadStart(this.T1));
t1.Start();
}
}
private void t2sr_Click(object sender, EventArgs e)
{
if (t2 == null)
{
t2 = new Thread(new ThreadStart(this.T2));
t2.Start();
}
}
private void t3sr_Click(object sender, EventArgs e)
{
if (t3 == null)
{
t3 = new Thread(new ThreadStart(this.T3));
t3.Start();
}
}
private void t1sp_Click(object sender, EventArgs e)
{
if (t1 != null)
{
t1.Abort();
t1 = null;
}
}
private void t2sp_Click(object sender, EventArgs e)
{
if (t2 != null)
{
t2.Abort();
t2 = null;
}
}
private void t3sp_Click(object sender, EventArgs e)
{
if (t3 != null)
{
t3.Abort();
t3 = null;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (t1 != null)
{
t1.Abort();
t1 = null;
}
if (t2 != null)
{
t2.Abort();
t2 = null;
}
if (t3 != null)
{
t3.Abort();
t3 = null;
}
}
private void ClearScreen_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
}
Hiç yorum yok:
Yorum Gönder