Ha - this age old issue.
As far as I can tell, there is no way to guarantee that a thread.abort() will ever actually be able to stop the thread process. It is much better to stop the threaded process yourself and wait for the thread to exit.
Something like this (this is how I do it atm):
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ThreadTest
{
public partial class Form1 : Form
{
Thread thTest;
public Form1()
{
thTest = new Thread(new ParameterizedThreadStart(ThreadTest));
}
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
_cancelThread = true;
if(_busy)
_resetEvent.WaitOne();
thTest = new Thread(new ParameterizedThreadStart(ThreadTest));
_resetEvent = new ManualResetEvent(false);
i = 0;
thTest.Start();
}
ManualResetEvent _resetEvent;
bool _cancelThread;
bool _busy;
void ThreadTest(object oState)
{
_busy = true;
_cancelThread = false;
while (!_cancelThread)
{
try
{
Console.WriteLine((i++).ToString());
System.Threading.Thread.Sleep(0);
}
catch (Exception e)
{
throw e;
}
}
_busy = false;
_resetEvent.Set();
}
}
}
Check out this article which goes over some of these principles:
Beginner's Guide to Threading in .NET: Part 3 of n[
^]
Hope that helps ^_^
Andy