namespace FrameworkTest
{
public partial class Form1 : Form
{
private bool stopThread = false;
delegate void InsertListCallback(int index, object item);
private void btnStart_Click(object sender, EventArgs e)
{
stopThread = false;
Thread t = new Thread(ThreadProc);
t.IsBackground = true;
t.Start();
}
private void ThreadProc()
{
int i = 0;
while (!stopThread)
{
this.Invoke(new InsertListCallback(
listBox1.Items.Insert),
new object[] { 0, i.ToString() });
Thread.Sleep(20);
i++;
}
if (stopThread)
this.Invoke(new InsertListCallback(
listBox1.Items.Insert),
new object[] { 0, "thread was stoped" });
}
private void btnStop_Click(object sender, EventArgs e)
{
stopThread = true;
}
}
}