471,305 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Asynchronous events exception handling

I have a remoting object, lets call it A, that fires an event to remotely
registered handlers, lets call it B.
The invocation is working OK and the remote handler B is handling the event
like it should.

But I need A to catch any exception thrown from the handler B, and also I
need that A will get the returned value returned by handler B.
For that I wrote:

private void ResultsHandler(IAsyncResult ar)
{
// Extract the delegate from the AsyncResult.
MyEventHandler handler =
(MyEventHandler)((System.Runtime.Remoting.Messagin g.AsyncResult)ar).AsyncDelegate;

// Obtain the result which I do not have because the delegate is void,
// but still need to that in order to catch exception caused in the.
handler.EndInvoke(ar);
}

public void RemtinMethod(long[] IDs)
{
// I'm using the AsyncCallback and state Object to be able to catch
exception caused in the delegate asynchronous invocation.
AsyncCallback cb = new AsyncCallback(ResultsHandler);
Object state = new Object();

if( RemoveDefectHandler != null )
{
foreach( MyEventHandler handler in MyEventHandler.GetInvocationList() )
{
try
{
handler.BeginInvoke(IDs, cb, state);
}
catch( Exception )
{
throw;
}
}
}
}
But still I can not get the returned value, and I'm also not catching at A
any thrown exception from B.

What I'm I doing wrong?

Is there any difference if I using the VS2003 or the VS2005? If so, what is
it?
--------
Thanks
Sharon
Jan 16 '06 #1
9 2183
Hello again,
I have some more info that may help.

(1) In the code I have posted I don't need the returned value.
(to get the returned value I just need to add: int result =
handler.EndInvoke(ar); at the ResultsHandler() )

(2) I can catch any exception thrown from B at the ResultsHandler(), but the
ResultsHandler() is invoked from another thread and not the invoking thread
(A that invoke the handler.BeginInvoke(IDs, cb, state);),
But I need to catch the exception at the calling thread A.
How do I do that?
---------
Thanks
Sharon
Jan 16 '06 #2
Hello Sharon,

How about catching exception just in B (ResultsHandler), and pass the
exception information back as result?

Luke

Jan 17 '06 #3
Thanks Luke,

That would be great.
But how can I do that? they are in a different thread.
---------
Thanks
Sharon
Jan 17 '06 #4
As you have posted:

to get the returned value I just need to add: int result =
handler.EndInvoke(ar); at the ResultsHandler() )

Luke

Jan 18 '06 #5
That's good. But I need to catch also the exceptions and that also can be
done at the ResultsHandler(). But the ResultsHandler() is invoked from
another thread then the thread the BegineInvoke() was called, and I need this
two values (returned value and caught exception) at the thread that invoked
the BegineInvoke() - calling thread A.

Is there a easy way to do that?
--
Thanks
Sharon
Jan 18 '06 #6
You may return a collection or array intead of a single object as returned
value. This is not perfect solution, but I don't there is a better one to
catch such an exception in another thread.

Luke

Jan 19 '06 #7
But it's impossible to just return, the ResultsHandler() does not return any
value and even if does, where this returned value will go to. As the
ResultsHandler() is in different thread then the thread I need the returned
value and exception.

There must be a comfortable way to do that. What is it?

-------
Thanks
Sharon
Jan 19 '06 #8
Hello Sharon,
But it's impossible to just return, the ResultsHandler() does not
return any value and even if does, where this returned value will go
to. As the ResultsHandler() is in different thread then the thread I
need the returned value and exception.

There must be a comfortable way to do that. What is it?


My preferred approach to communicate exceptions across threads is using events.
Assume you have two classes: class A uses an asynchronous service offered
by class B. B also has an "Error" event, for which A registers an event handler
before invoking any of B services. In all of B's relevant catch blocks, B
fires of its Error event to notify its clients (such as A) of an exception.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Jan 19 '06 #9
Communicating among thread is fine, but I just thought that the asynchronous
delegates like I'm using by Remoting has this mechanism built in to catch
exception and return value in the calling thread and not in a callback at
another thread.

-------
Thanks
Sharon
Jan 22 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Matthew Groch | last post: by
1 post views Thread by Natalia DeBow | last post: by
4 posts views Thread by rawCoder | last post: by
15 posts views Thread by Javier Estrada | last post: by
3 posts views Thread by Anders Both | last post: by
4 posts views Thread by taskswap | last post: by
1 post views Thread by jan.loucka | last post: by
2 posts views Thread by =?Utf-8?B?S2FseWFu?= | last post: by
reply views Thread by rosydwin | last post: by

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.