473,388 Members | 1,399 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,388 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 2881
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 project (potentially all of which will be written in...
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 exception object is created (because I can create...
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 are: 1. The code the initiates any high-level...
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 exception handling in it. What's happening is that...
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 classes. For example, let's say I send an update from...
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. An engineer aware of defects can avoid...
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 construct, are difficult to use correctly,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.