S
Soren S. Jorgensen
Hi,
In my app I've got a worker thread (background) doing some calculations
based upon user input. A new worker thread might be invoked before the
previous worker thread has ended, and I wan't only one worker thread running
at any time (if a new worker thread start has been requested, any running
worker thread results will be invalid). I'm using the below method to invoke
a new worker thread, but when stress testing this I'm sometimes getting a
System.Threading.ThreadStateException (Thread is suspended; attempting to
abort) and everything locks up.
Does anyone now of a more fail safe method to successfully kill a ongoing
thread ??
private void StartWorker()
{
lock(this)
{
if(_calcThread != null)
{
while(_calcThread.IsAlive)
{
if(_calcThread.ThreadState !=
System.Threading.ThreadState.AbortRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.SuspendRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.Suspended)
{
_calcThread.Abort();
}
else
{
_calcThread.Join(50);
}
}
_calcThread = null;
}
_calcThread = new Thread(new ThreadStart(this.RunCalculation));
_calcThread.IsBackground = true;
_calcThread.Priority = ThreadPriority.Highest;
_calcThread.Start();
}
Thanks...
Soren
In my app I've got a worker thread (background) doing some calculations
based upon user input. A new worker thread might be invoked before the
previous worker thread has ended, and I wan't only one worker thread running
at any time (if a new worker thread start has been requested, any running
worker thread results will be invalid). I'm using the below method to invoke
a new worker thread, but when stress testing this I'm sometimes getting a
System.Threading.ThreadStateException (Thread is suspended; attempting to
abort) and everything locks up.
Does anyone now of a more fail safe method to successfully kill a ongoing
thread ??
private void StartWorker()
{
lock(this)
{
if(_calcThread != null)
{
while(_calcThread.IsAlive)
{
if(_calcThread.ThreadState !=
System.Threading.ThreadState.AbortRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.SuspendRequested &&
_calcThread.ThreadState !=
System.Threading.ThreadState.Suspended)
{
_calcThread.Abort();
}
else
{
_calcThread.Join(50);
}
}
_calcThread = null;
}
_calcThread = new Thread(new ThreadStart(this.RunCalculation));
_calcThread.IsBackground = true;
_calcThread.Priority = ThreadPriority.Highest;
_calcThread.Start();
}
Thanks...
Soren