AutoResetEvent 类 处理线程等待问题

 1 [HttpGet(Name = "JsAdd")]
 2         public string JsAdd()
 3         {
 4 
 5             //autoResetEvent 属性设置为false 执行到WaitOne 进入等待
 6             AutoResetEvent autoResetEvent = new AutoResetEvent(false);
 7             //开启新线程处理业务
 8             Thread thread = new Thread(() =>
 9             {
10                 Task.Delay(1000).Wait();
11                 Console.WriteLine("此业务算出被加数值");
12                 //新线程业务处理完毕,从WaitOne下方继续执行
13                 autoResetEvent.Set();
14             });
15             thread.Start();
16             //执行本线程业务
17             Console.WriteLine("此业务算出加数值");
18             //等待新线程业务处理完毕
19             autoResetEvent.WaitOne();
20             //执行加数与被加数相加  此处加数结果视为1 被加数结果视为2
21             var testSer = MefHelper.Ins.service.GetService<ITestService>();
22             var result = "结果:" + testSer.GetAddResult(1, 2);
23 
24             return result;
25         }

参考地址:https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.autoresetevent?view=net-7.0

                  https://blog.csdn.net/weixin_43702146/article/details/125785343

posted @ 2023-05-09 09:57  Fast & Furious  阅读(27)  评论(0)    收藏  举报