C#轮询模式不懂

Tony_Xian 2017-09-16 07:17:43
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ParallelForeach1
{
delegate long MyDel(int first,int second);
class Program
{
static long Sum(int x, int y)
{
Console.WriteLine(" Inside Sum");
Thread.Sleep(100);
return x + y;
}
static void Main()
{
MyDel del = new MyDel(Sum);
IAsyncResult iar = del.BeginInvoke(3, 5, null, null);
Console.WriteLine("After BeginInvoke");
while (!iar.IsCompleted)
{
Console.WriteLine("Not Done");
Console.WriteLine("Hello World");
}
Console.WriteLine("Done");
long result = del.EndInvoke(iar);
Console.WriteLine("Result: {0}", result);
Console.Read();
}
}
}


运行会出现两个版本
第一种

第二中

大神来解答下原因,谢谢!
...全文
410 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ilikeff8 2017-09-18
  • 打赏
  • 举报
回复
加个延迟

while (!iar.IsCompleted)
{
Thread.Sleep(10);

Console.WriteLine("Not Done");
Console.WriteLine("Hello World");
}

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧