Deve sempre chamar EndInvoke um delegado dentro AsyncCallback?
Eu li oUse AsyncCallback, existe este trecho de código:
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;
class MainClass
{
delegate int MyDelegate(string s);
static void Main(string[] args)
{
MyDelegate X = new MyDelegate(DoSomething);
AsyncCallback cb = new AsyncCallback(DoSomething2);
IAsyncResult ar = X.BeginInvoke("Hello", cb, null);
Console.WriteLine("Press any key to continue");
Console.ReadLine();
}
static int DoSomething(string s)
{
Console.WriteLine("doooooooooooooooo");
return 0;
}
static void DoSomething2(IAsyncResult ar)
{
MyDelegate X = (MyDelegate)((AsyncResult)ar).AsyncDelegate;
X.EndInvoke(ar);
}
}
Note que emDoSomething2
, que é umAsyncCallback
, o delegado é explicitamente morto pelo comandoEndInvoke
.
Isso é realmente necessário? PorqueAsyncCallback
não será chamado a menos que e até que o método delegado seja executado.