Why does thread remain AbortRequested?

B

Brett

I have a second thread in which I call t.abort() from the parent thread. I
see "t"s state as AbortRequested. It stays this way for some time. Under
which conditions will a thread remain this way?

The sub procedure that "t" calls is an inifite loop with code inside. Near
the end of each interation, this sub() will call currentthread.sleep(x).
That works fine. I imagine the parent thread can't abort the child thread,
"t", until the inifinite loop finishes, which it never will.

If I request a t.suspend() or t.sleep() from the parent thread then
t.abort(), does that allow the thread to abort or do I need to interrupt the
thread?

Thanks,
Brett
 
H

Herfried K. Wagner [MVP]

Brett said:
I have a second thread in which I call t.abort() from the parent thread. I
see "t"s state as AbortRequested. It stays this way for some time. Under
which conditions will a thread remain this way?

Call 't.Join()' immediately after calling 't.Abort' if you need to block the
current thread until the aborted thread died.
The sub procedure that "t" calls is an inifite loop with code inside.

In this case, using a boolean variable that is checked by the thread (for
example, every n-th iteration of the loop) is IMO the preferred way. More
information:

How To Stop a Thread in .NET (and Why 'Thread.Abort' is Evil)
<URL:http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation>

Shutting down worker threads gracefully
<URL:http://www.yoda.arachsys.com/csharp/multithreading.html#worker.threads>
 
B

Brett

Herfried K. Wagner said:
Call 't.Join()' immediately after calling 't.Abort' if you need to block
the current thread until the aborted thread died.


In this case, using a boolean variable that is checked by the thread (for
example, every n-th iteration of the loop) is IMO the preferred way. More
information:

How To Stop a Thread in .NET (and Why 'Thread.Abort' is Evil)
<URL:http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation>

Shutting down worker threads gracefully
<URL:http://www.yoda.arachsys.com/csharp/multithreading.html#worker.threads>

Thanks for the links and I will try the boolean method you mention.

When I'm debugging an app with two threads, I can see it sometimes will
switch from the class file back to the form every other line, depending on
where I am. Is it true multi threaded apps don't actually run threads in
parallel? They just take turns with small slices of execution time.

Some processors execution instructions in parallel. Wouldn't this allow two
threads to run in parallel rather than share time?

Thanks,
Brett
 
H

Herfried K. Wagner [MVP]

Brett,

Brett said:
When I'm debugging an app with two threads, I can see it sometimes will
switch from the class file back to the form every other line, depending on
where I am. Is it true multi threaded apps don't actually run threads in
parallel? They just take turns with small slices of execution time.

That's typical for multithreaded applications.
Some processors execution instructions in parallel. Wouldn't this allow
two threads to run in parallel rather than share time?

On a single-processor machine, multiple threads run pseudo-parallel, which
means that each thread has its timeslice. If you run the application on a
multithreading system, threads can be executed by different CPUs.
 
B

Brett

Herfried K. Wagner said:
Brett,



That's typical for multithreaded applications.


On a single-processor machine, multiple threads run pseudo-parallel, which
means that each thread has its timeslice. If you run the application on a
multithreading system, threads can be executed by different CPUs.
Ok. Than what does it mean to execute instructions in parallel within the
cpu on a single cpu machine? If they can only execute one at a time (or is
that reserved to threads only), what does "parallel" mean? Is something
different going on at the processor level than the threading level?

Also, a thread is multiple processor instructions correct?

Thanks,
Brett
 
B

Brett

Maybe I'm not understanding how the boolean is used. In Form1, I'm doing
this on application shutdown:

DoProcess = 0
If (t.ThreadState.ToString = "SuspendedRequested,
WaitSleepJoin") Or (t.ThreadState.ToString = "Suspended") Or
(t.ThreadState.ToString = "WaitSleepJoin, Suspended") Then
Else
t.Abort()
End If
Me.Close()

In Class1, which is spawned by the thread "t", I have this for the boolean
on the loop:

While Form1.DoProcess = 1

On Form1, I can stop "t" by doing a t.suspend(). That's where the problem
is. Now if I try to shut down the app after doing t.suspend(), the above
code runs and the first IF condition is hit, which is to do nothing. Then
Form1 closes. The debugger stop button is still visible and my cpu is at
100%. Meaning thread "t" is still going.

How do I go about killing "t" after calling a t.suspend?

If I don't do t.suspend() and just shut down, the t.abort() condition works
fine and "t" is dies. The app shuts down ok. During this time, "t" is in a
"WaitSleepJoin, Suspended" or "Running" state.

Should I also add this after the loop in Class1:
Thread.CurrentThread.Abort()

Thanks,
Brett
 
B

Brett

This doesn't seem correct for a non HT processor. They show one thread
executing then another only after the first. It should be they execute in
parallel but take turns...never executing at the same time.

Brett
 
J

Jay B. Harlow [MVP - Outlook]

Brett,
Have you tried setting the Thread.IsBackground property to True? This causes
the thread to terminate when your main (UI) thread terminates, without any
extra code!


Rather then compare your thread state to Strings I would suggest comparing
it to the actual enum values:

Something like:

If (theThread.ThreadState = (ThreadState.SuspendRequested Or
ThreadState.WaitSleepJoin)) _
Or (theThread.ThreadState = ThreadState.Suspended) _
Or (theThread.ThreadState = (ThreadState.WaitSleepJoin Or
ThreadState.Suspended)) Then
Else
theThread.Abort()
End If

Alternatively you can check to see if individual flags are on:

If (theThread.ThreadState And ThreadState.SuspendRequested) =
ThreadState.SuspendRequested _
Or (theThread.ThreadState And ThreadState.Suspended) =
ThreadState.Suspended _
Or (theThread.ThreadState And ThreadState.WaitSleepJoin) =
ThreadState.WaitSleepJoin Then
Else
theThread.Abort()
End If

Sometimes to simplify the above I will create a function.

If IsThreadState(theThread, ThreadState.SuspendRequested) _
Or IsThreadState(theThread, ThreadState.Suspended) _
Or IsThreadState(theThread, ThreadState.WaitSleepJoin) Then

End If

Private Function IsThreadState(ByVal theThread As Thread, ByVal theState
As ThreadState) As Boolean
Return ((theThread.ThreadState And theState) = theState)
End Function

Hope this helps
Jay

Brett said:
Maybe I'm not understanding how the boolean is used. In Form1, I'm doing
this on application shutdown:

DoProcess = 0
If (t.ThreadState.ToString = "SuspendedRequested,
WaitSleepJoin") Or (t.ThreadState.ToString = "Suspended") Or
(t.ThreadState.ToString = "WaitSleepJoin, Suspended") Then
Else
t.Abort()
End If
Me.Close()

In Class1, which is spawned by the thread "t", I have this for the boolean
on the loop:

While Form1.DoProcess = 1

On Form1, I can stop "t" by doing a t.suspend(). That's where the problem
is. Now if I try to shut down the app after doing t.suspend(), the above
code runs and the first IF condition is hit, which is to do nothing. Then
Form1 closes. The debugger stop button is still visible and my cpu is at
100%. Meaning thread "t" is still going.

How do I go about killing "t" after calling a t.suspend?

If I don't do t.suspend() and just shut down, the t.abort() condition
works fine and "t" is dies. The app shuts down ok. During this time, "t"
is in a "WaitSleepJoin, Suspended" or "Running" state.

Should I also add this after the loop in Class1:
Thread.CurrentThread.Abort()

Thanks,
Brett
 
B

Brett

Thanks Jay. That does help.

Brett

Jay B. Harlow said:
Brett,
Have you tried setting the Thread.IsBackground property to True? This
causes the thread to terminate when your main (UI) thread terminates,
without any extra code!


Rather then compare your thread state to Strings I would suggest comparing
it to the actual enum values:

Something like:

If (theThread.ThreadState = (ThreadState.SuspendRequested Or
ThreadState.WaitSleepJoin)) _
Or (theThread.ThreadState = ThreadState.Suspended) _
Or (theThread.ThreadState = (ThreadState.WaitSleepJoin Or
ThreadState.Suspended)) Then
Else
theThread.Abort()
End If

Alternatively you can check to see if individual flags are on:

If (theThread.ThreadState And ThreadState.SuspendRequested) =
ThreadState.SuspendRequested _
Or (theThread.ThreadState And ThreadState.Suspended) =
ThreadState.Suspended _
Or (theThread.ThreadState And ThreadState.WaitSleepJoin) =
ThreadState.WaitSleepJoin Then
Else
theThread.Abort()
End If

Sometimes to simplify the above I will create a function.

If IsThreadState(theThread, ThreadState.SuspendRequested) _
Or IsThreadState(theThread, ThreadState.Suspended) _
Or IsThreadState(theThread, ThreadState.WaitSleepJoin) Then

End If

Private Function IsThreadState(ByVal theThread As Thread, ByVal
theState As ThreadState) As Boolean
Return ((theThread.ThreadState And theState) = theState)
End Function

Hope this helps
Jay
 
C

Cor Ligthert

Brett,

On a true single processor is never something executed the same time
everything is sequential. (A hyperthreading processor is in fact acting as a
dual processor). That is the reason that I write always in this newsgroups
that with multithreading you can only win time when there are stops in the
delivery of external data (and that has not as reason again own processor
actions).

Good samples to win time with multithreading can be downloading from more
sites. Direct printing to (a) printer(s).

In all other situations multithreading will cost more time because of the
fact that multithreading has to be processed itself.

When there are non dependent processes (what is quit seldom in
dataprocessing, however think about advantage games) than can it be as well
help to shorten time.

Just my thought,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top