异步委托主要用于解决?ThreadPool.QueueUserWorkItem
?没有提供获取线程执行完成后的返回值问题。异步委托只能在.Net Framework 框架下使用,.Net Core中会报平台错误,而且使用Task.Result来获取返回值,可以达成同样的目的;本文纯粹是做个验证试验。
static void Main(string[] args)
{
AsyncResultTest();
Console.WriteLine("OK");
Console.ReadKey();
}
public static void AsyncResultTest()
{
var str = "hello";
var testOb = new TestOb() { msg = str };
Func<TestOb, int> testMethod = Dosomething;
//testMethod.BeginInvoke(testOb, Done, testMethod);
var asyncResult = testMethod.BeginInvoke(testOb, null, null);
var task01 = Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
testOb.msg = "hello world";
Console.WriteLine($"task01 exceuted str is {testOb.msg}");
});
task01.Wait();
var result = testMethod.EndInvoke(asyncResult);
Console.WriteLine($"AsyncResultTest finished, result is {result}");
}
private static int Dosomething(TestOb testOb)
{
Console.WriteLine($"Dosomething:{testOb.msg}");
Thread.Sleep(2000);
return testOb.msg.Length;
}
private static void Done(IAsyncResult result)
{
var asyncState = (Func<TestOb, int>)result.AsyncState;
var retVal = asyncState.EndInvoke(result);
Console.WriteLine($"result is:{retVal}");
}
class TestOb
{
public string msg { get; set; }
}
执行结果
.Net Core 下实现类似效果的代码:
public static void TaskReturnValueTest()
{
var value = "000";
var task01 = Task.Factory.StartNew(() =>
{
value = "111";
Thread.Sleep(1000);
Console.WriteLine($"task01 exceuted value is {value}.");
return "1";
});
var task02 = Task.Factory.StartNew(() =>
{
Thread.Sleep(800);
value = "222";
Console.WriteLine($"task02 exceuted value is {value}.");
return "2";
});
Task.WaitAll(task01, task02);
var ret01 = task01.Result;
var ret02 = task02.Result;
Console.WriteLine($"task01: {ret01}, task02: {ret02}");
Console.WriteLine("Ok");
}
执行结果:?
用Task类库的代码实现方式明显更优,更易于理解和维护。