473,757 Members | 5,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on processes 'n' threads #2

Hi

I have a service which starts extra threads which take a long time, this is so it can respond to events while long processes are running. I was just wondering,

a) if an exception is thrown in a worker thread and it is unhandled, does it bring the whole application down or just the worker thread that suffered it?

b) if an exception is thrown in a function which doesn't have exception handling but the function that called it does, will the exception simply propogate up until it finds a handler, and then consider that handler to be the official handler of that exception?

Thanks
Nov 16 '05 #1
9 1348
Beeeeeeeeeeeeve s,

See inline:

"Beeeeeeeeeeeev es" <Be************ **@discussions. microsoft.com> wrote in
message news:2B******** *************** ***********@mic rosoft.com...
Hi

I have a service which starts extra threads which take a long time, this is so it can respond to events while long processes are running. I was just
wondering,
a) if an exception is thrown in a worker thread and it is unhandled, does it bring the whole application down or just the worker thread that suffered
it?

I believe that the application will shut down, as the exception is
unhandled

b) if an exception is thrown in a function which doesn't have exception handling but the function that called it does, will the exception simply
propogate up until it finds a handler, and then consider that handler to be
the official handler of that exception?

If there is not an exception handler in the function that the exception
was thrown in, then the exception will bubble up the call stack until it is
handled, or, the application terminates.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

Thanks

Nov 16 '05 #2
Hi,
I have a service which starts extra threads which take a long time, this is so it can respond to events while long processes are running. I was

just wondering,

a) if an exception is thrown in a worker thread and it is unhandled,
does it bring the whole application down or just the worker thread that suffered it?

I believe that the application will shut down, as the exception is
unhandled


I believe that only the thread where the exception happen is kill, the
others will continue to run, .

I just found this on an article in MSDN:

Here is a summary of the unhandled exception handler default behaviors that
are executing in this application.
a.. Unhandled exceptions that occur on the application's main thread cause
the application to terminate.
b.. Unhandled exceptions that occur in threads other than the
application's main thread are swallowed by the CLR. This includes manual
threads, thread pool threads, and the CLR's finalizer thread. If your
application is a console application, the CLR outputs exception text to the
console (though your application keeps running). If your application is not
a console application, there is no outward indication when an exception
occurs and your application keeps running.
c.. Unhandled exceptions that occur on a thread that is pumping window
messages via the Windows Forms classes are subject to the Windows Forms
unhandled exception handler. A debug dialog box is produced by default, but
this behavior can be overridden (more on this in a moment).

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #3

"Beeeeeeeeeeeev es" <Be************ **@discussions. microsoft.com> wrote in
message news:02******** *************** ***********@mic rosoft.com...
so, "best not to rely on it", then..?


So how can I handle a situation in which I want to show a error message to
the user when one of my child thread throws a exception?

regards
Kiran
Nov 16 '05 #4
Kiran,

If this is the case, then you will have to marshal a message to the UI
thread (through a call to Invoke on a control that was created on that UI
thread) which would then cause a message to appear.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Kiran" <ki****@abc.com > wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...

"Beeeeeeeeeeeev es" <Be************ **@discussions. microsoft.com> wrote in
message news:02******** *************** ***********@mic rosoft.com...
so, "best not to rely on it", then..?


So how can I handle a situation in which I want to show a error message to
the user when one of my child thread throws a exception?

regards
Kiran

Nov 16 '05 #5
That would depend on how you are creating your threads. If you are creating
the thread by hand (i.e. calling Thread.Start()) then the best way is to add
a try/catch block to the start function of the thread. Catch the exception
and log the exception at that point.

Generally it's bad practice to swallow System.Exceptio n but at the point you
will be catching it the CLR is just about to swallow it as well. FxCop will
still complain.

If you are using other asynchronous methods that will create additional
threads, you have to handle those on a case by case basis

--
Jared Parson [MSFT]
ja******@online .microsoft.com

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Kiran" <ki****@abc.com > wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...

"Beeeeeeeeeeeev es" <Be************ **@discussions. microsoft.com> wrote in
message news:02******** *************** ***********@mic rosoft.com...
so, "best not to rely on it", then..?


So how can I handle a situation in which I want to show a error message to
the user when one of my child thread throws a exception?

regards
Kiran

Nov 16 '05 #6
"Beeeeeeeeeeeev es" <Be************ **@discussions. microsoft.com> wrote in
message news:02******** *************** ***********@mic rosoft.com...
so, "best not to rely on it", then..?

It has the same effect as returning from the thread. You could for instance
not catch the ThreadInterrupt edException exception when you want to shut
down your thread using Thread::Interru pt.

Cheers,
--
Tom Tempelaere
Nov 16 '05 #7
I do shut down threads, but using Thread.Abort() followed by Thread.Join(),
but in this program I do catch the ThreadAbortExce ption that is generated.
But in my other program I was just trying to weigh up whether it was worth
putting handlers in the worker thread functions, when there might have been
some rule that states they can't bring down the main app anyway... but it
seems people are divided
"TT (Tom Tempelaere)" <_N_0SPA|/\|t*******@hotm ail.com|/\|APS0_N_> wrote in
message news:vq******** **************@ phobos.telenet-ops.be...
"Beeeeeeeeeeeev es" <Be************ **@discussions. microsoft.com> wrote in
message news:02******** *************** ***********@mic rosoft.com...
so, "best not to rely on it", then..?

It has the same effect as returning from the thread. You could for

instance not catch the ThreadInterrupt edException exception when you want to shut
down your thread using Thread::Interru pt.

Cheers,
--
Tom Tempelaere

Nov 16 '05 #8
Nicholas ,

OK I got around the problem using
"AppDomain.Curr entDomain.Unhan dledException" which catches exception from
any thread when its not handled there.

you see any problem with this???

Regards
Kiran.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:uD******** ******@TK2MSFTN GP10.phx.gbl...
Kiran,

If this is the case, then you will have to marshal a message to the UI
thread (through a call to Invoke on a control that was created on that UI
thread) which would then cause a message to appear.


Nov 16 '05 #9

"Beeeeeves" <12345512345789 5123456789> wrote in message
news:OK******** ******@tk2msftn gp13.phx.gbl...
I do shut down threads, but using Thread.Abort() followed by Thread.Join(), but in this program I do catch the ThreadAbortExce ption that is generated.
But in my other program I was just trying to weigh up whether it was worth
putting handlers in the worker thread functions, when there might have been some rule that states they can't bring down the main app anyway... but it
seems people are divided

I believe it is worth putting handlers in all worker threads. Here's my take
on it...

It's easy to do.

Even though the current CLR implementation does not bring down an app when a
manual thread has an unhandled exception, this may change in future
releases. This is a policy decision that the app itself should make, not the
runtime.

An unhandled exception means that something unexpected went wrong and the
app not only did not do something about it, it did not even notice - this
cannot be a good thing, and is likely to be a very bad thing. It is often
the source of many subtle bugs, and can also be the source of major bugs. It
can result in an app becoming partially hung - the UI may be responsive but
the main thread may be waiting for an operation to complete that never will.

If you are using asynchronous IO in a worker thread (e.g. calling a
webservice), then all methods that call EndInvoke with the AsyncResult
object should be prepared to deal with an exception, because this may
rethrow the original exception that occurred during processing and was
swallowed.

IMO if exceptions are used as intended you should never get one unless
something has gone wrong. This argues that the application should do
something meaningful with it, and this usually indicates that it should at
least write an entry to the event log (or do something similar) to leave
enough breadcrumbs behind for support to help diagnose the problem.
Obviously there are always deviations from this, but I find it to be a
useful guide.

I define an unhandled exception handler to record that one occurred so that
if I get one I know where to start looking in the code - it means that
there's a chunk of code somewhere that needs to have a handler.

Unhandled exceptions in finalizers and dispose methods are especially
troubling and should be treated as major problems. Some cleanup scenarios
are complicated and necessary, and for something to go wrong in one usually
indicates a problem that needs to be addressed. It may also lead to leaks,
and if dealing with unmanaged code/COM, etc. the leaks may extend beyond the
boundaries of the .net runtime.

IMO, I think it was a bad decision to allow unhandled exceptions to not
crash an app. In Win32 an unhandled exception, in any thread, crashed the
app, and I think that was a better way to go.


Nov 16 '05 #10

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

Similar topics

1
1852
by: Michael Williams | last post by:
Hi, I am trying to understand the performance implications of running a number of separate ActiveXexe processes as opposed to a single ActiveXexe with multiple threads on a Windows 2000 server. I have been told that Windows does not allocate a fixed memory space to its processes, so they are always paged off to disk when inactive. However, all the threads in a single process operate within the same memory space, so are relatively...
8
4567
by: AnalogKid | last post by:
Short question: What's the difference between SingleUse and MultiUse ? Long question: I've been writing some sample code to see how different Instancing values and threading models work. I tried all main combinations of Instancing (Multiuse/Singleuse) and of Threading models (per object/pool). Here's what I found: MultiUse - Thread per Object
3
7650
by: Thomas Schmid | last post by:
Hi there, I wrote a tcp server which listens on a port. When he gets a new connection, he starts a new thread like this: thread.start_new_thread(self.ConnectionHandler, (conn,)) where conn is the socket for the connection. After I started the server, I can see one running process under Linux, which is ok. But once I connected to the Server, I always see two processes where one is the parent of the other. I think this should be
11
12719
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to read the new date. So process B starts "select ..." but does not get the previously inserted row. The timespan between commit and select is very short. (NOTE: two different sessions are used) Questions: 1.) Does commit when returning from call...
11
4269
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
0
1182
by: Stephen Barrett | last post by:
After reading through the many many many posts on threading, I still don't quite understand how to accomplish a task. Here is an overview of the app I am coding. I am sorry if it is a little long. A Windows Service app (no probs here) that basically needs to look for work to do from 1 to many MSMQ queues that it then needs to pass on to 1 to many (configurable) worker threads to process. The worker thread actually runs a few...
5
2491
by: Jeremy | last post by:
I have a core VB service that monitors a database, and based on data in the records will execute code to send email notifications. Problem: I don't want my main program code to halt and wait for this execution... we need immediate execution of all notification processes and would prefer to spawn multiple instances generating emails at one time. Solution ? I took a look at threading, and at first it looked to be the answer.
2
1481
by: Abubakar | last post by:
Hi, in my application, different threads send notifications to GUI thread through sendmessage api. They have special integer numbers inside the wParam arg of sendmessage which the gui thread uses to display messages in the textboxes etc. My question is that in case the threads send messages very fast (which does NOT always happen in my app), faster than the gui thread is processing them, are the messages qued automatically (btw the gui...
35
4032
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? I've been leaning towards threads, I'm going to say why. Processes seem fairly expensive from my research so far. Each fork copies the entire contents of memory into the new process. There's also a more expensive context switch between...
9
1339
by: Carl Johansson | last post by:
If a multithreaded .NET application is executed on a computer with a multicore processor. Will the application automatically run the threads on different processor cores? Regards Carl Johansson
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8736
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.