OK, there are a few recommendations:
Use flying flag
... , .
volatile bool stopThread = false;
IsBackground true: , , " ", .
thread1.IsBackground = true;
thread1.Start();
, , ... , Abort, :
... Abort , . , , , finally . , . , .
Abort
, , Interrupt :
private void btnStop_Click(object sender, EventArgs e)
{
StopThread();
}
private void StopThread()
{
stopThread = true;
thread1.Join(1100);
if(thread1.IsAlive)
{
thread1.Interrupt();
}
}
, ""... thread1 , , . , .
private void btnStart_Click(object sender, EventArgs e)
{
StopThread();
stopThread = false;
thread1 = new Thread(new ThreadStart(disp));
thread1.IsBackground = true;
thread1.Start();
}
, :
void disp()
{
try
{
while (stopThread == false)
{
listBox1.Invoke(tickerDelegate1, new object[] { DateTime.Now.ToString() });
Thread.Sleep(1000);
}
}
catch(ThreadInterruptedException)
{
}
}
, ... ... , : !;)