全部 -
我有一个使用回调模式的异步委托的示例代码。我正在使用标准代表,下面的代码可以正常工作。想将它转换为 Func 代表(因为它期望和 int 并返回一个 int)但由于某种原因 - 似乎卡住了。有人可以帮忙吗。
class Program
{
public delegate int SomeDelegate(int x);
static void Main(string[] args)
{
Console.WriteLine("This is the CallBack Pattern Technique for Asynchronous Delegates in Threading");
Console.WriteLine("");
SomeDelegate sd = SquareNumber;
Console.WriteLine("[{0}] Before SquareNumber Method invoked.", Thread.CurrentThread.ManagedThreadId);
IAsyncResult asyncRes = sd.BeginInvoke(10, new AsyncCallback(CallbackMethod), null);
Console.WriteLine("[{0}] Back in Main after SquareNumber Method invoked. Doing Extra Processing.", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("[{0}] Main method processing completed.", Thread.CurrentThread.ManagedThreadId);
Console.ReadLine();
}
static int SquareNumber(int a)
{
Console.WriteLine("[{0}] Inside SquareNumber - invoked. Processing......", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(5000);
return a * a;
}
static void CallbackMethod(IAsyncResult asyncRes)
{
Console.WriteLine("[{0}] Callback Invoked", Thread.CurrentThread.ManagedThreadId);
AsyncResult ares = (AsyncResult)asyncRes;
SomeDelegate delg = (SomeDelegate)ares.AsyncDelegate;
int res = delg.EndInvoke(asyncRes);
Console.WriteLine("[{1}] Result = {0}", res, Thread.CurrentThread.ManagedThreadId);
}
}
谢谢