472,328 Members | 1,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 3902
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ken Godee | last post by:
module1 calls a function in module2 module2 starts a thread that calls a function in module3 and then returns to module1 thread finishes and I...
5
by: Jon Perez | last post by:
Running the following under Linux creates 3 processes instead of 2. Once the started thread exits, 2 processes still remain. Why? import...
4
by: Daylor | last post by:
in win32 process , when u create new process,u have new main thread. i know,appDomain r logical procces,that exists in 1 win32 process. the q:...
4
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm...
4
by: Ivan | last post by:
Hi All, my question is how to teminate a running thread... it sounds simple but in fact i 'm having problem with it firstly, i create a new...
11
by: Michael Moreno | last post by:
Hello, I have a C# server which has 4 worker threads running all the time. When I let the server runs for several hours, for some reasons the CPU...
4
by: Mufasa | last post by:
Is there any way to force a thread to abort and really have it abort? I have a thread that every once in a while gets hung to so I kill it. Problem...
1
by: NvrBst | last post by:
Is there any internal thread variables/states I can set to request stops? or I need to make my own global variables? IE...
65
by: Aditya | last post by:
Hi I am using a line of as char recvedValues = {'\0'}; in my code. I get a warning as near initialization for recvedValues. I am using gcc 3.4...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.