I am writing a .NET library. One of the classes has events a user of the library will need to subscribe to. Is it OK to ask implementations of handlers for these events return quickly? Or is this a common problem for which there is a common solution?
(It will no be fatal if the handler took long - but things start to wrong if their handler takes longer than about half a second - it is a networking library and peers connected will think this peer has dropped as the event is raised on the same thread for sending replies)
e.g.
public delegate void Ping();
class A
{
/// <summary>
/// If your handler doesn't return quickly... I am going to cry.
/// </summary>
public event Ping Ping;
private void RaisePing()
{
var handler = Ping;
if(handler != null)
handler();
}
// this is called several times a secound
private void MainLoop()
{
if(something)
RaisePing();
// time important stuff - musn't take long to get here...
}
}