Working with events
-
Hi there, I would like to notify my app if a socketconnection is broken. I tried to use a self created event, with wich i want to notify an upper class in such a case. The problem is i receive a unhandled exception of type 'System.MissingMethodException'... Why? What Am i doing wrong and what means this error? Thanks a lot, stonee
internal delegate void ConnectionHandler(SocketException se); ............ public class Control { internal event ConnectionHandler OnConnectionProblem; ................ public Control() { OnConnectionProblem+=new ConnectionHandler(ESControl_OnConnectionProblem); .................... catch(SocketException se) { / MessageBox.Show (se.Message ); try { OnConnectionProblem(se); } catch(MissingMethodException mme) { MessageBox.Show(mme.Message+"::"+mme.InnerException); }
-
Hi there, I would like to notify my app if a socketconnection is broken. I tried to use a self created event, with wich i want to notify an upper class in such a case. The problem is i receive a unhandled exception of type 'System.MissingMethodException'... Why? What Am i doing wrong and what means this error? Thanks a lot, stonee
internal delegate void ConnectionHandler(SocketException se); ............ public class Control { internal event ConnectionHandler OnConnectionProblem; ................ public Control() { OnConnectionProblem+=new ConnectionHandler(ESControl_OnConnectionProblem); .................... catch(SocketException se) { / MessageBox.Show (se.Message ); try { OnConnectionProblem(se); } catch(MissingMethodException mme) { MessageBox.Show(mme.Message+"::"+mme.InnerException); }
Here's the problem: stonee74 wrote: OnConnectionProblem+=new ConnectionHandler(ESControl_OnConnectionProblem); There's no method
ESControl_OnConnectionProblem
Here's how I would do it:// this defines the event delegate
public delegate void ConnectionHandler(object sender, SocketException se);
// this keeps all event arguments
public class ConnectionProblemEventArgs
{
private SocketException m_SocketException;
public ConnectionProblemEventArgs(SocketException se)
{
SocketException = se;
}
public SocketException SocketException
{
get
{ return m_SocketException; }
}
}
public class EsControl
{
// here's the event definition
public event ConnectionHandler ConnectionProblem;// this raises the event public virtual void OnConnectionProblem(SocketException se) { if (ConnectionProblem != null) ConnectionProblem (this, new ConnectionProblemEventArgs (se)); } private DoSomethingWithSocket() { try { ... } catch(SocketException se) { // raise the event OnConnectionProblem (se); } }
}
public class EventListner
{
public EventListner(EsControl esControl)
{
// subscribe to event here
EsControl.ConnectionProblem += new ConnectionHandler (ESControl_ConnectionProblem);
}private void ESControl\_ConnectionProblem(object sender, ConnectionProblemEventArgs e) { // Handle event here }
}
α.γεεκ
Fortune passes everywhere.
Duke Leto Atreides -
Here's the problem: stonee74 wrote: OnConnectionProblem+=new ConnectionHandler(ESControl_OnConnectionProblem); There's no method
ESControl_OnConnectionProblem
Here's how I would do it:// this defines the event delegate
public delegate void ConnectionHandler(object sender, SocketException se);
// this keeps all event arguments
public class ConnectionProblemEventArgs
{
private SocketException m_SocketException;
public ConnectionProblemEventArgs(SocketException se)
{
SocketException = se;
}
public SocketException SocketException
{
get
{ return m_SocketException; }
}
}
public class EsControl
{
// here's the event definition
public event ConnectionHandler ConnectionProblem;// this raises the event public virtual void OnConnectionProblem(SocketException se) { if (ConnectionProblem != null) ConnectionProblem (this, new ConnectionProblemEventArgs (se)); } private DoSomethingWithSocket() { try { ... } catch(SocketException se) { // raise the event OnConnectionProblem (se); } }
}
public class EventListner
{
public EventListner(EsControl esControl)
{
// subscribe to event here
EsControl.ConnectionProblem += new ConnectionHandler (ESControl_ConnectionProblem);
}private void ESControl\_ConnectionProblem(object sender, ConnectionProblemEventArgs e) { // Handle event here }
}
α.γεεκ
Fortune passes everywhere.
Duke Leto Atreides