前言

回忆一下以前是怎么写线程的。这种方式.net core 已经不支持了,所以看看就好,或者有些老项目中使用。

正文

private static int Calculate(int a, int b)
  {
	  System.Threading.Thread.Sleep(1000 * 10);//假如计算需要3秒钟

	  int c = a + b;

	  Console.WriteLine("\r\n计算完成,结果:{0}+{1}={2}", a, b, c);

	  return c;
 }
static void Main(string[] args)
 {
	 Console.WriteLine("-----------程序运行开始----------");

	 Func<int, int, int> action = Calculate;//声明一个委托

	IAsyncResult ret = action.BeginInvoke(1, 2, null, null);

	 Console.WriteLine("我不参与计算,先走了啊!");

	 Console.WriteLine("正在努力计算:");
	 while (ret.IsCompleted == false)
	 {
		 Console.Write(".");
		 System.Threading.Thread.Sleep(1000);
	 }

	 int amount = action.EndInvoke(ret);

	 Console.WriteLine("-----------程序运行结束----------");

	 Console.ReadKey();
 }

相关文章:

  • 2021-08-11
  • 2021-12-31
  • 2021-07-15
  • 2021-05-29
  • 2021-06-25
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-30
  • 2021-09-07
  • 2021-06-14
相关资源
相似解决方案