使用Func帶返回值委託,異步調用
使用EndInvoke阻塞進程,獲取委託函數返回值
Func<int> func = ()=>
{
Thread.Sleep(600);
Console.WriteLine("funtion processing");
return DateTime.Now.Day;
};
IAsyncResult result=null;
result = func.BeginInvoke(asyncResult =>
{
Console.WriteLine(object.ReferenceEquals(result, asyncResult));
//回調方法體
Thread.Sleep(1000);
Console.WriteLine($"當前進程號:{Thread.CurrentThread.ManagedThreadId}");
}, null);
//注意:每個異步操作只能調用一次EndInvoke
int day = func.EndInvoke(result);//獲取fun委託函數返回值
Console.WriteLine("func result value:"+day);
Console.WriteLine("over");