469,927 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Why does thread remain AbortRequested?

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
Nov 21 '05 #1
10 3815
"Brett" <no@spam.com> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e%****************@tk2msftngp13.phx.gbl...
"Brett" <no@spam.com> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


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
Nov 21 '05 #3
Brett,

"Brett" <no@spam.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e$**************@TK2MSFTNGP15.phx.gbl...
Brett,

"Brett" <no@spam.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

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
Nov 21 '05 #5
Brett,

It takes only some minutes

http://www.intel.com/personal/do_mor...s/03b_base.swf

I hope that this gives you some idea's

Cor
Nov 21 '05 #6
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e$**************@TK2MSFTNGP15.phx.gbl...
Brett,

"Brett" <no@spam.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:um**************@TK2MSFTNGP09.phx.gbl...
Brett,

It takes only some minutes

http://www.intel.com/personal/do_mor...s/03b_base.swf

I hope that this gives you some idea's

Cor

Nov 21 '05 #8
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" <no@spam.com> wrote in message
news:eA**************@TK2MSFTNGP15.phx.gbl...
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e$**************@TK2MSFTNGP15.phx.gbl...
Brett,

"Brett" <no@spam.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Nov 21 '05 #9
Thanks Jay. That does help.

Brett

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
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" <no@spam.com> wrote in message
news:eA**************@TK2MSFTNGP15.phx.gbl...
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e$**************@TK2MSFTNGP15.phx.gbl...
Brett,

"Brett" <no@spam.net> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



Nov 21 '05 #10
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
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Ken Godee | last post: by
11 posts views Thread by Michael Moreno | last post: by
4 posts views Thread by Mufasa | last post: by
1 post views Thread by NvrBst | last post: by
65 posts views Thread by Aditya | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.