C#中 同步方法调用异步方法不死锁的方法 async await task

 

//同步方法调用异步方法
public static string Test(string cName) 
{
    AutoResetEvent autoResetEvent = new AutoResetEvent(false);
    ThreadPool.QueueUserWorkItem(async (str) =>
    {
        var custom = cusService.SelectOne(c => c.CUSTOM_NAME == cName).GetAwaiter().GetResult();
     autoResetEvent.Set(); // 工作完成后设置信号

    }
    var b = autoResetEvent.WaitOne(); // 等待信号

}

  

posted @ 2024-01-09 16:52  iDEAAM  阅读(50)  评论(0编辑  收藏  举报