472,328 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

exception handling with events

Hi there,

I am working on an client-server app, where the client asynchronously issues
a request for the server to perform some action and the server is supposed
to notify the client when the action was performed and send the data back by
raising an event.

My question is how do I catch exceptions on the client side that can
potentially be thrown while the server is trying to perform an action, since
the client used BeginInvoke to spawn a worker thread when making this
function call. Also, I've read that for every BeginInvoke, there should be
an EndInvoke to release the worker threads back to the thread pool.
How/where would I use EndInvoke in my scenario? I really don't have a
callback right now, since I am using the event handling method and the data
is being returned via the EventArgs object in the event and not using the
result of the AsyncResult.

What would be the best approach to handle exceptions on the client side with
events and where would I include EndInvoke?

Client code:
-----------------
public class Client
{...
....
public void MakeServerDoAction(...)
{
try
{
server.fireEvent += new EventHandler(OnEventFired);
server.DoAction.BeginInvoke (param1, param2, null, null);
}
catch (Exception e)
{...
}
}

public void OnEventFired(object sender, EventArgs e)
{...
....
// display info from EventArgs on the UI.
}
}
Share data:
-------------
public class DataTypes
{
public event EventHandler fireEvent;
public delegate void EventHandler(object sender, EventArgs e);
}

Server code:
-------------------
pubic class Server
{
public void DoAction(int param1, double param2)
{
// do some stuff
InvokeDone(data);
}
internal void InvokeDone(object data)
{
if (fireEvent != null)
foreach (Delegate del in fireEvent.GetInvokationList)
{
try
{
....
}
catch (Exception e)
{...
}
}

Thanks so much in advance,
Natalia
Nov 16 '05 #1
2 2831
You must call EndInvoke to avoid memory leaks.
The good news is that any exception that your DoAction threw will be
thrown when you call EndInvoke (that's so cool!)

So when shoud you call EndInvoke?

The second to last argument to BeginInvoke is an AsyncCallback that
will be called when your function has completed. You can use this to
call EndInvoke.

Of course you need the delegate to fire EndInvoke. The delegate is
hidden inside the argument passed to your AsyncCallback method.
IAsyncResult does not contain the delegate but it can be safely cast to
AsyncResult (in the System.Runtime.Remoting.Messaging namespace) this
contains the delegate which
you can then use to call EndInvoke.

You should call EndInvoke in a try/catch block to catch any exceptions
your asynchronous method threw.

Your AsyncCallback executes on the thread pool thread so don't directly
access UI from the callback (use control.BeginInvoke).

Instead of :
server.DoAction.BeginInvoke (param1, param2, null, null);

use :
Server.DoAction.BeginInvome(param1, param2, new
AsyncCallback(ActionCompleted), null);

void ActionCompleted(IAsyncResult iar)
{
System.Runtime.Remoting.Messaging.AsyncResult ar = iar as
System.Runtime.Remoting.Messaging.AsyncResult;
if (iar != null)
{
EventHandler eh = ar.AsyncDelegate as EventHandler;

if (eh != null)
{
try
{
eh.EndInvoke();
}
catch(Exception ex)
{
// the exception thrown by Server.DoAction
// process the exception
// If you want to display it on the ui make sure to use
Control.BeginInvoke since this routine is executing on the thread pool.
}
}
}
}

Nov 16 '05 #2
Hi DanGo,

Thanks so much for your response! Your solution is viable, however, it
requires me to create a callback method "Action Completed" that I really
don't need for anything other than exception handling. From what I've
read, I should only use callbacks if I need to get to the result of the
asynchronously executed method. In my case, I don't care about the
result, since the valuable data is being returned via event handling
mechanism. So, this poses another question, is there any other way to
catch exceptions from the worker thread other than creating a callback
and calling EndInvoke from it? Is there any other place where I could
call EndInvoke from (for instance, from the same method that called
BeginInvoke) and what would be the drawbacks of this approach?

Thanks again,
Natalia

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

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

Similar topics

7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
28
by: Scott Brady Drummonds | last post by:
Hi, all, I just got out of a meeting with a team of software developers that I recently joined as they are staffing to create a medium-sized...
14
by: Nenad Dobrilovic | last post by:
Hi, Is it possible for exception object to be aware of it's throwing? I want to log in the text file when exeption is thrown, not when the...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
6
by: Robin Riley | last post by:
Hi, I have a .NET solution that contains a dll project and a tester application project which, of course, invokes the dll. The dll project has...
24
by: Earl | last post by:
I have all of my data operations in a separate library, so I'm looking for what might be termed "best practices" on a return type from those...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects....
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.