473,287 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 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 3985
"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 need the return value from the thread to use in...
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 thread from thread import start_new_thread def...
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: is there way to create second appDomain (the...
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 using a thread to receive data from the network, that...
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 thread (refer to follow example code) and start it...
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 usage of the application will shoot to 100% and...
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 is I have a thread that every once in a while...
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 ----------------------------- Thread myThrd = new Thread(new...
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 Can anybody please explain the meaning. Thanks...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.