Asynchronous events exception handling

G

Guest

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.Messaging.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?
 
G

Guest

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?
 
L

Luke Zhang [MSFT]

Hello Sharon,

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

Luke
 
G

Guest

Thanks Luke,

That would be great.
But how can I do that? they are in a different thread.
 
L

Luke Zhang [MSFT]

As you have posted:

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

Luke
 
G

Guest

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?
 
L

Luke Zhang [MSFT]

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
 
G

Guest

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?
 
J

Joerg Jooss

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,
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top