I have looked at all the solutions for this topic but still cannot seem to accomplish the stopping of a thread without using Thread.Abort(). Here is the code: Main class code that creates the the thread:
_pollingThread = new Thread(pollingThread);
_pollingThread.Start();
And here is the thread:
void _pollingThread()
{
while (continuePolling) //initially set to true
{
//do some things can take about 200ms to run
}
}
I next attempt to stop the thread from the main thread by setting continuePolling to false.
private void form1_FormClosing(object sender, FormClosingEventArgs e)
{
continuePolling = false;
Thread.Sleep(1000);
_pollingThread.Join(1000);
if (_pollingThread.IsAlive) //always alive!
{
_pollingThread.Abort;
}
}
Can someone tell me what I am doing wrong? Thanks