473,573 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.s leep(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 3998
"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.arachs ys.com/csharp/multithreading. html#worker.thr eads>

--
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%******** ********@tk2msf tngp13.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.arachs ys.com/csharp/multithreading. html#worker.thr eads>

--
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$******** ******@TK2MSFTN GP15.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 = "SuspendedReque sted,
WaitSleepJoin") Or (t.ThreadState. ToString = "Suspended" ) Or
(t.ThreadState. ToString = "WaitSleepJ oin, 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
"WaitSleepJ oin, Suspended" or "Running" state.

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

Thanks,
Brett

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:e$******** ******@TK2MSFTN GP15.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******** ******@TK2MSFTN GP09.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.IsBackgr ound 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.Thre adState = (ThreadState.Su spendRequested Or
ThreadState.Wai tSleepJoin)) _
Or (theThread.Thre adState = ThreadState.Sus pended) _
Or (theThread.Thre adState = (ThreadState.Wa itSleepJoin Or
ThreadState.Sus pended)) Then
Else
theThread.Abort ()
End If

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

If (theThread.Thre adState And ThreadState.Sus pendRequested) =
ThreadState.Sus pendRequested _
Or (theThread.Thre adState And ThreadState.Sus pended) =
ThreadState.Sus pended _
Or (theThread.Thre adState And ThreadState.Wai tSleepJoin) =
ThreadState.Wai tSleepJoin Then
Else
theThread.Abort ()
End If

Sometimes to simplify the above I will create a function.

If IsThreadState(t heThread, ThreadState.Sus pendRequested) _
Or IsThreadState(t heThread, ThreadState.Sus pended) _
Or IsThreadState(t heThread, ThreadState.Wai tSleepJoin) Then

End If

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

Hope this helps
Jay

"Brett" <no@spam.com> wrote in message
news:eA******** ******@TK2MSFTN GP15.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 = "SuspendedReque sted,
WaitSleepJoin") Or (t.ThreadState. ToString = "Suspended" ) Or
(t.ThreadState. ToString = "WaitSleepJ oin, 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 "WaitSleepJ oin, Suspended" or "Running" state.

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

Thanks,
Brett

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:e$******** ******@TK2MSFTN GP15.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******** ******@TK2MSFTN GP09.phx.gbl...
Brett,
Have you tried setting the Thread.IsBackgr ound 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.Thre adState = (ThreadState.Su spendRequested Or
ThreadState.Wai tSleepJoin)) _
Or (theThread.Thre adState = ThreadState.Sus pended) _
Or (theThread.Thre adState = (ThreadState.Wa itSleepJoin Or
ThreadState.Sus pended)) Then
Else
theThread.Abort ()
End If

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

If (theThread.Thre adState And ThreadState.Sus pendRequested) =
ThreadState.Sus pendRequested _
Or (theThread.Thre adState And ThreadState.Sus pended) =
ThreadState.Sus pended _
Or (theThread.Thre adState And ThreadState.Wai tSleepJoin) =
ThreadState.Wai tSleepJoin Then
Else
theThread.Abort ()
End If

Sometimes to simplify the above I will create a function.

If IsThreadState(t heThread, ThreadState.Sus pendRequested) _
Or IsThreadState(t heThread, ThreadState.Sus pended) _
Or IsThreadState(t heThread, ThreadState.Wai tSleepJoin) Then

End If

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

Hope this helps
Jay

"Brett" <no@spam.com> wrote in message
news:eA******** ******@TK2MSFTN GP15.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 = "SuspendedReque sted,
WaitSleepJoin") Or (t.ThreadState. ToString = "Suspended" ) Or
(t.ThreadState. ToString = "WaitSleepJ oin, 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 "WaitSleepJ oin, Suspended" or "Running" state.

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

Thanks,
Brett

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:e$******** ******@TK2MSFTN GP15.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2935
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 module1 where program flow is continuing. err, I hope I explained that well enough?
5
2703
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 newthread(): print "child"
4
6191
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 first 1 is the defualt appDomain) with his new ("main") thread ? if not ,what is the easy and clear way to create new thread on the new appDomain ?
4
7358
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 I suspend and resume whenver needed. In order to properly shut down the program every thread has to be aborted. So, I override...
4
1094
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 on form load once the form button on clicked... i try terminate the thread... but an exception was catched...
11
6041
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 remain there. I would like to find out which thread(s) is/are the source of the problem. Would you have any idea on how to tackle the problem...
4
15873
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 gets stuck (I'm working on why that happens) and I want to kill it. I do a thread.abort and the status becomes abortrequested but never actually goes...
1
1485
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 ParameterizedThreadStart(Func)); myThrd.Start(objParams); *do stuff* myThrd.ThreadState = ThreadState.SuspendRequested; *do more stuff*
65
3555
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 Aditya
0
7705
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7796
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6424
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5294
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1310
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.