Since you wanted asynchronous execution of your authentication methods, you could work like this.
Create a delegate that returns a bool
public delegate bool Authenticate(object sender, AuthenticateEventArgs e);
Authenticate authHandler;
You may or may not use the arguments but you can use or remove that later.
Create your authentication methods
bool AuthenticationMethod1(object o, AuthenticateEventArgs ea)
{
System.Threading.Thread.Sleep(2000); //Simulate some long running task.
return false; //Return true or false based on authentication failed or succeeded.
}
bool AuthenticationMethod2(object o, AuthenticateEventArgs ea)
{
System.Threading.Thread.Sleep(2000); //Simulate some long running task.
return true; //Return true or false based on authentication failed or succeeded.
}
bool AuthenticationMethod3(object o, AuthenticateEventArgs ea)
{
System.Threading.Thread.Sleep(2000); //Simulate some long running task.
return false; //Return true or false based on authentication failed or succeeded.
}
Hook up the handlers
authHandler += AuthenticationMethod1;
authHandler += AuthenticationMethod2;
authHandler += AuthenticationMethod3;
Now execute
if (authHandler != null)
{
foreach (Authenticate handler in authHandler.GetInvocationList())
{
handler.BeginInvoke(this, e as AuthenticateEventArgs, new AsyncCallback(Callback), handler);
}
}
LAST PART : Have you callback defined
void Callback(IAsyncResult ar)
{
Authenticate d = (Authenticate)ar.AsyncState;
if (d.EndInvoke(ar))
{
OnLoggedIn(new EventArgs());
}
else
{
OnLoggedError(new EventArgs());
}
}