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