Thread Abort
-
Hi! How can we code to make assure that if the thread we want to abort is processing a function, it just be aborted after finish that function ? Thanks!
Here is one way of stopping your thread, making sure your current execution ends and you do not get an AbortException. You would have something like this to establish the thread in your main processor:
ThreadClass function = new ThreadClass(); // your process
myThread = new ThreadStart(function.Process);your Process would be something like this:
public bool ThreadWanted = true;
... init/constructor logic ...
do
{
if ( ..no data to process.. )
Thread.Sleep(50);
...function
}
while (ThreadWanted)
return;when you want the thread to abort just do this in the thread creator logic:
public void TerminateProcess()
{
function.ThreadWanted = false;
}There are 10 kinds of people in the world.
Those that read binary...
...and those who don't. -
Here is one way of stopping your thread, making sure your current execution ends and you do not get an AbortException. You would have something like this to establish the thread in your main processor:
ThreadClass function = new ThreadClass(); // your process
myThread = new ThreadStart(function.Process);your Process would be something like this:
public bool ThreadWanted = true;
... init/constructor logic ...
do
{
if ( ..no data to process.. )
Thread.Sleep(50);
...function
}
while (ThreadWanted)
return;when you want the thread to abort just do this in the thread creator logic:
public void TerminateProcess()
{
function.ThreadWanted = false;
}There are 10 kinds of people in the world.
Those that read binary...
...and those who don't. -
Here is one way of stopping your thread, making sure your current execution ends and you do not get an AbortException. You would have something like this to establish the thread in your main processor:
ThreadClass function = new ThreadClass(); // your process
myThread = new ThreadStart(function.Process);your Process would be something like this:
public bool ThreadWanted = true;
... init/constructor logic ...
do
{
if ( ..no data to process.. )
Thread.Sleep(50);
...function
}
while (ThreadWanted)
return;when you want the thread to abort just do this in the thread creator logic:
public void TerminateProcess()
{
function.ThreadWanted = false;
}There are 10 kinds of people in the world.
Those that read binary...
...and those who don't.You need to declare the bool variable with the volatile qualifier, without it, there is no guarantee that setting it to true would immediately reflect in the other thread. Regards Senthil _____________________________ My Blog | My Articles | WinMacro