473,405 Members | 2,344 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,405 software developers and data experts.

Delegates and Exceptions

AC
Hi All,

I have been experimenting with delegates and exceptions.
I know that when an exception occurs on a separate thread
it is saved and retrieved when EndInvoke is called, but
why can't I rethrow it?

Regards,

Example Code:

class Application
{
delegate void MyDelegate();

static void Main(string[] args)
{
Application app = new Application();
app.Start();

Console.ReadLine();
}
public void Start()
{
MyDelegate myDelegate = new MyDelegate(BadMethod);

myDelegate.BeginInvoke(new AsyncCallback
(DelegateCallback), myDelegate);
}
public void BadMethod()
{
string newstring = string.Empty;

if (newstring.Length == 0)
{
throw new ApplicationException("delegate
testing exception");
}
}
private void DelegateCallback(IAsyncResult ar)
{
MyDelegate result = (MyDelegate) ar.AsyncState;

try
{
result.EndInvoke(ar);
}
catch
{
throw;
}
}
}
Jul 21 '05 #1
12 1537
AC <an*******@discussions.microsoft.com> wrote:
I have been experimenting with delegates and exceptions.
I know that when an exception occurs on a separate thread
it is saved and retrieved when EndInvoke is called, but
why can't I rethrow it?


You can - what makes you think you can't?

Note that in your example, when your exception is rethrown, nothing is
reporting it because it's in a threadpool thread without any extra
exception handlers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
AC
Hi Jon,
Thanks for the reply. How would I catch exceptions
from the thread pool? I've tried registering a handler
for the AppDomain.UnhandledException to no avail.

Thanks
-----Original Message-----
AC <an*******@discussions.microsoft.com> wrote:
I have been experimenting with delegates and exceptions. I know that when an exception occurs on a separate thread it is saved and retrieved when EndInvoke is called, but why can't I rethrow it?
You can - what makes you think you can't?

Note that in your example, when your exception is

rethrown, nothing isreporting it because it's in a threadpool thread without any extraexception handlers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Jul 21 '05 #3

"AC" <an*******@discussions.microsoft.com> wrote in message
news:11****************************@phx.gbl...
Hi Jon,
Thanks for the reply. How would I catch exceptions
from the thread pool? I've tried registering a handler
for the AppDomain.UnhandledException to no avail.

The threadpool itself catches and swallows all exceptions thrown on its
threads, so you will not get an unhandled exception. You need to catch all
the exceptions thrown in your threadpool callback routine and then
propagate that exception using your own mechanism. You can queue it up,
report it immediately, etc.
Jul 21 '05 #4
AC
Sorry I guess I'm being thick here. But that was what I
wanted to do in the first place. In the callback routine
I simple wanted to throw the exception so it gets
propagated up the call stack.

Would it be possible to point me at a simple example?

Thanks again.
-----Original Message-----

"AC" <an*******@discussions.microsoft.com> wrote in messagenews:11****************************@phx.gbl...
Hi Jon,
Thanks for the reply. How would I catch exceptions
from the thread pool? I've tried registering a handler
for the AppDomain.UnhandledException to no avail.
The threadpool itself catches and swallows all

exceptions thrown on itsthreads, so you will not get an unhandled exception. You need to catch allthe exceptions thrown in your threadpool callback routine and thenpropagate that exception using your own mechanism. You can queue it up,report it immediately, etc.
.

Jul 21 '05 #5
AC <an*******@discussions.microsoft.com> wrote:
Sorry I guess I'm being thick here. But that was what I
wanted to do in the first place. In the callback routine
I simple wanted to throw the exception so it gets
propagated up the call stack.

Would it be possible to point me at a simple example?


Which thread's stack are you expecting it to get propagated up?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
DT
I thought that because the callback executes on the main thread in my
example I could propagate the exception on that thread.

I guess my new question is how to detect exceptions in the threadpool?

Thanks again

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
AC <an*******@discussions.microsoft.com> wrote:
Sorry I guess I'm being thick here. But that was what I
wanted to do in the first place. In the callback routine
I simple wanted to throw the exception so it gets
propagated up the call stack.

Would it be possible to point me at a simple example?


Which thread's stack are you expecting it to get propagated up?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #7
DT <dt@nospam.co.uk> wrote:
I thought that because the callback executes on the main thread in my
example I could propagate the exception on that thread.
No - the callback *doesn't* execute on the main thread. It can't,
because the main thread is busy doing something else by then - that's
the point of it being an asynchronous call in the first place.
I guess my new question is how to detect exceptions in the threadpool?


Well, you've got your callback - you can put whatever code you want in
there, including a big try/catch round whatever else is normally there.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8
DT
But the callback isn't the asynch process? Isn't the whole concept of a
callback to notify the caller that the asynch process has completed?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
DT <dt@nospam.co.uk> wrote:
I thought that because the callback executes on the main thread in my
example I could propagate the exception on that thread.


No - the callback *doesn't* execute on the main thread. It can't,
because the main thread is busy doing something else by then - that's
the point of it being an asynchronous call in the first place.
I guess my new question is how to detect exceptions in the threadpool?


Well, you've got your callback - you can put whatever code you want in
there, including a big try/catch round whatever else is normally there.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #9
DT <dt@nospam.co.uk> wrote:
But the callback isn't the asynch process? Isn't the whole concept of a
callback to notify the caller that the asynch process has completed?


No, it's to allow something to happen after the main async process has
completed. That "something" may involve the caller, but it may not.

Put it this way - after your call to BeginInvoke, the main thread goes
on its merry way and executes some more code. What would you expect the
stack trace to look like in the main thread when the callback was
invokved? Would you expect it to just interrupt the main thread
wherever it was? The nightmare of re-entrancy would be horrible in that
case.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10
DT
I didn't realise that. Thanks for your help

Regards

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
DT <dt@nospam.co.uk> wrote:
But the callback isn't the asynch process? Isn't the whole concept of a
callback to notify the caller that the asynch process has completed?


No, it's to allow something to happen after the main async process has
completed. That "something" may involve the caller, but it may not.

Put it this way - after your call to BeginInvoke, the main thread goes
on its merry way and executes some more code. What would you expect the
stack trace to look like in the main thread when the callback was
invokved? Would you expect it to just interrupt the main thread
wherever it was? The nightmare of re-entrancy would be horrible in that
case.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #11
If you just want to capture the exception information you can do something
like this...wrap your callback with a try-catch and in the catch block
simply save the exception object into a queue; this executes in the context
of the threadpool thread. You can then set a flag or an event to notify the
main thread that new data is available and it can pull the exception object
out of the queue.

"DT" <dt@nospam.co.uk> wrote in message
news:41**********************@mercury.nildram.net. ..
I didn't realise that. Thanks for your help

Regards

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
DT <dt@nospam.co.uk> wrote:
But the callback isn't the asynch process? Isn't the whole concept of a
callback to notify the caller that the asynch process has completed?


No, it's to allow something to happen after the main async process has
completed. That "something" may involve the caller, but it may not.

Put it this way - after your call to BeginInvoke, the main thread goes
on its merry way and executes some more code. What would you expect the
stack trace to look like in the main thread when the callback was
invokved? Would you expect it to just interrupt the main thread
wherever it was? The nightmare of re-entrancy would be horrible in that
case.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jul 21 '05 #12
DT
Thanks David

"David Levine" <no******************@wi.rr.com> wrote in message
news:ua**************@TK2MSFTNGP12.phx.gbl...
If you just want to capture the exception information you can do something
like this...wrap your callback with a try-catch and in the catch block
simply save the exception object into a queue; this executes in the
context of the threadpool thread. You can then set a flag or an event to
notify the main thread that new data is available and it can pull the
exception object out of the queue.

"DT" <dt@nospam.co.uk> wrote in message
news:41**********************@mercury.nildram.net. ..
I didn't realise that. Thanks for your help

Regards

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
DT <dt@nospam.co.uk> wrote:
But the callback isn't the asynch process? Isn't the whole concept of
a
callback to notify the caller that the asynch process has completed?

No, it's to allow something to happen after the main async process has
completed. That "something" may involve the caller, but it may not.

Put it this way - after your call to BeginInvoke, the main thread goes
on its merry way and executes some more code. What would you expect the
stack trace to look like in the main thread when the callback was
invokved? Would you expect it to just interrupt the main thread
wherever it was? The nightmare of re-entrancy would be horrible in that
case.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Jul 21 '05 #13

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

Similar topics

1
by: 0to60 | last post by:
More importantly, WHY do I wanna do this? Well, I have events that I raise asynchronously like this: foreach(eventhandler handler in eventname.getinvocationlist()) handler.begininvoke(); ...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
12
by: AC | last post by:
Hi All, I have been experimenting with delegates and exceptions. I know that when an exception occurs on a separate thread it is saved and retrieved when EndInvoke is called, but why can't I...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
3
by: rbjorkquist | last post by:
I am looking for some help figuring out the best way to refactor these two examples. I was thinking of using delegates and/or generics; but have little experience with either. Any sample code...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
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
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...

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.