473,426 Members | 1,603 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,426 software developers and data experts.

How to terminate sub-thread processes

Hi

I make an app which can run some sub processes through multiple threads. I'd like to know how to terminate all sub-threads when the main thread is closed

thanks in advance
Nov 20 '05 #1
9 7399
Hi Li Pang

When you create the thread, set IsBackground = True before you start it
running. That way it will be terminated when the owning thread terminates.

HTH

Charles
Li Pang wrote:
Hi,

I make an app which can run some sub processes through multiple
threads. I'd like to know how to terminate all sub-threads when the
main thread is closed.

thanks in advance

Nov 20 '05 #2
* "=?Utf-8?B?TGkgUGFuZw==?=" <an*******@discussions.microsoft.com> scripsit:
I make an app which can run some sub processes through multiple
threads. I'd like to know how to terminate all sub-threads when the main
thread is closed.


What are the sub threads doing? You can set a Boolean variable that
tells the threads to exit.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
thanks
Nov 20 '05 #4
Hi Charles,

Delights a lot of questions about this I think, thanks from me too.
When you create the thread, set IsBackground = True before you start it
running. That way it will be terminated when the owning thread terminates.

Charles

Nov 20 '05 #5
On Wed, 26 May 2004 11:12:19 +0100, Charles Law wrote:
Hi Li Pang

When you create the thread, set IsBackground = True before you start it
running. That way it will be terminated when the owning thread terminates.

HTH

Charles


Charles,

I'm not so sure this is a good idea... I have to check, but I believe if
you do this then the threads are forcibly terminated - which means that
they are not given an opertunity to run any clean up code. Personally, I
would set a flag to signal the threads to terminate, and then wait for the
children to die before exiting.

--
Tom Shelton [MVP]
Nov 20 '05 #6
Hi Tom,

:-(

However I think you are right when I read this.

So I have nothng to change

:-)

Cor
I'm not so sure this is a good idea... I have to check, but I believe if
you do this then the threads are forcibly terminated - which means that
they are not given an opertunity to run any clean up code. Personally, I
would set a flag to signal the threads to terminate, and then wait for the
children to die before exiting.

Nov 20 '05 #7
Hi Tom

Yes, I should have been more specific. This isn't a good replacement for
terminating a thread in an orderly manner. That said, I have found recently
that using a flag to indicate that the thread does not always work. My
example is where I set my flag to false (to terminate the thread) and then
use thread Join to wait for it to terminate. It never does. If I allow the
application to run on, or use a DoEvents (ugh!), then the thread terminates.
I now use the thread Abort method and trap the exception raised in the
thread. It's not pretty, but it works, and permits some sort of graceful
exit.

Charles
[Incidently, I currently have a problem based on the non-terminating issue
above, that involves SyncLock. There are three threads, including the main
UI thread, all trying to send and receive commands down a serial port. All
commands go out through a single function, making it easier to protect.
However, while the two background threads are hammering away at sending and
receiving commands, if I try to send a command from the UI the UI thread
freezes trying to get a lock on my command queue. Once it does that I can't
find out what the other threads are doing in the debugger, as any
breakpoints I set in the body of the threads are never hit. I'm not sure I
have given you enough to go on, but any suggestions about debugging
multi-threaded apps greatly received.]
Tom Shelton wrote:
On Wed, 26 May 2004 11:12:19 +0100, Charles Law wrote:
Hi Li Pang

When you create the thread, set IsBackground = True before you start
it running. That way it will be terminated when the owning thread
terminates.

HTH

Charles


Charles,

I'm not so sure this is a good idea... I have to check, but I
believe if you do this then the threads are forcibly terminated -
which means that they are not given an opertunity to run any clean up
code. Personally, I would set a flag to signal the threads to
terminate, and then wait for the children to die before exiting.

Nov 20 '05 #8
Tom,
The background thread will have Thread.Abort called, which causes an
Exception to be raised on that thread.

http://msdn.microsoft.com/library/de...roundTopic.asp

http://msdn.microsoft.com/library/de...AbortTopic.asp

If the background thread does its cleanup in Try/Finally blocks (which I
sincerely hope it does) it will clean up nicely.

However! I agree, most of the time I would set a signal to have the worker
threads clean up.

Hope this helps
Jay

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:m3******************************@40tude.net.. .
On Wed, 26 May 2004 11:12:19 +0100, Charles Law wrote:
Hi Li Pang

When you create the thread, set IsBackground = True before you start it
running. That way it will be terminated when the owning thread terminates.
HTH

Charles


Charles,

I'm not so sure this is a good idea... I have to check, but I believe if
you do this then the threads are forcibly terminated - which means that
they are not given an opertunity to run any clean up code. Personally, I
would set a flag to signal the threads to terminate, and then wait for the
children to die before exiting.

--
Tom Shelton [MVP]

Nov 20 '05 #9
In article <OQ**************@tk2msftngp13.phx.gbl>, Jay B. Harlow [MVP - Outlook] wrote:
Tom,
The background thread will have Thread.Abort called, which causes an
Exception to be raised on that thread.

http://msdn.microsoft.com/library/de...roundTopic.asp

http://msdn.microsoft.com/library/de...AbortTopic.asp


Good to know that the Abort is called - except that in this situation,
I'm not necessarily sure that cleanup will occur. The reason I say that
is that Thread.Abort does not necessarily throw the ThreadAbort
exception immediately... It really depends on the state of the thread
when the abort is called. The main exception being unmanaged code, the
exception will not be thrown until the code re-enters managed code - and
if that is a blocking call, such as those called by the socket classes
:), that could be for quite some time. I'm assuming that the system,
will kill forcibly any threads that do not exit in a timely fashion. I
could be wrong, but it would be interesting to know the answer here.
I've always avoided background threads, just on the principle that I
should make sure that I'm doing what needs to be done :)
--
Tom Shelton [MVP]
Nov 20 '05 #10

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

Similar topics

3
by: abovetreeline | last post by:
Hi, How to make javascript terminate a win32_process? vbscript will terminate a named process with the following code: strcomputer = "." Set objwmiservice = GetObject("winmgmts:" _ &...
4
by: deko | last post by:
I use the default windows phone dialer to make calls from an Access form. I close the phone dialer after the call is made using the below function. The problem is I'm not closing the Phone Dialer...
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
22
by: Vell | last post by:
Hi, i'm moving my first steps in vb.NET world and i'm getting first troubles.. in vb6 i can start an application from Sub Main() and load for example a form using 'set' and 'show' command. The...
17
by: Fred Nelson | last post by:
Hi: I'm trying to implement an overall error handler in my VB.NET windows application. I need to place an "On Error Goto" in "Sub Main". I have tried to create a Sub Main and have had no...
3
by: schapopa | last post by:
I am working on upgrading my application from VB 6.0 to .net and I have module set as a startup. There is a warning that application will terminate when Sub Main finishes and recommendation to move...
7
by: fyi85 | last post by:
I have 8.1.5 on Windows 2003, when I do from CLP with auto commit off: db2 update table set column=something and then db2 terminate and then db2 connect to db db2 select updated column from...
2
by: Draw | last post by:
I have an application, where I do connect, handle a few requests, and if for some reason, I lose connectivity, I detect that and loop(trying to reconnect) until I regain connection to the DB. My...
1
by: Justin | last post by:
We had a load that failed. Now we have a pending load. When executing load terminate, we receive the following error: db2 load terminate SQL0104N An unexpected token "terminate" was found...
7
by: blublu123 | last post by:
Hi all, I have a number of fields on various forms that need data validation. If fields are empty (null), the form should not save and close, and the cursor should go to the appropriate field. I...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.