I start a thread when pressing on start button which start a delay timer and then show a messageBox dialog. Now, I'm trying to stop this thread, but I can't find a way for that, except add a flag which will prevent the thread to display the messageBox dialog but not to kill the thread. I would appreciate if you can suggest a way to KILL the thread.
Thanks Moti
public partial class Form1 : Form
{
public delegate void example();
ThreadA threadA = null;
public Form1()
{
InitializeComponent();
}
example ex;
IAsyncResult result;
private void button_Start_Click(object sender, EventArgs e)
{
ex = new example(start);//.BeginInvoke(null, null);
result = ex.BeginInvoke(null, null);
}
private void button_Stop_Click(object sender, EventArgs e)
{
if (threadA != null)
threadA = null;
}
private void start()
{
if (threadA == null)
{
threadA = new ThreadA();
threadA.run();
}
}
}
class ThreadA
{
//public event
public Boolean flag = false;
public void run()
{
Thread.Sleep(15000);
MessageBox.Show("Ended");
}
}