同步方法调用异步方法
.GetAwaiter().GetResult()
计算函数超时,异步方法所有的回调操作都会期望返回到主线程。
所以会导致各种线程死锁。异步方法中使用ConfigureAwait(false)解决
1 public void Check(){ //todo 2 //验证userid和token是否匹配 3 var tUserId = UserIdAndTokenValidationAsync(userId, at.UserId); 4 //验证参数签名是否正确 5 Task<bool> tvalid = CheckSignatureAsync(paramList, signature, at.Token); 6 //验证请求时间 7 Task<bool> tTicks = TicksValidationAsync(signature, timestamp); 8 9 bool userIdRes = tUserId.GetAwaiter().GetResult(); 10 bool validRes = tvalid.GetAwaiter().GetResult(); 11 bool ticksRes = tTicks.GetAwaiter().GetResult(); 12 }
public async Task<bool> UserIdAndTokenValidationAsync(int requsetUserId, int tokenUserId) { bool res = await Task.Factory.StartNew(() => { return UserIdAndTokenValidation(requsetUserId, tokenUserId); }).ConfigureAwait(false); return res; }
原文地址:https://www.cnblogs.com/Jerrycjc/p/9050307.html
时间: 2024-10-15 04:19:49