C# 第二个函数在第一个等待函数之后未执行
我使用async await调用两个方法。但只有第一个是执行。我希望这两个函数应该异步运行。因为第二个函数的输入不依赖于第一个函数的执行。有人能告诉我我到底错过了什么吗 第一个函数执行成功,执行第一个函数时没有发生异常 以下是有关我的代码的详细信息:C# 第二个函数在第一个等待函数之后未执行,c#,async-await,C#,Async Await,我使用async await调用两个方法。但只有第一个是执行。我希望这两个函数应该异步运行。因为第二个函数的输入不依赖于第一个函数的执行。有人能告诉我我到底错过了什么吗 第一个函数执行成功,执行第一个函数时没有发生异常 以下是有关我的代码的详细信息: if (MoveCardEntity.Error.Code == Constant.API_ERROR_CODE_SUCCESS) if (LoggedInUserSession.CustomerConfig
if (MoveCardEntity.Error.Code == Constant.API_ERROR_CODE_SUCCESS)
if (LoggedInUserSession.CustomerConfigPermission.IsNotificationsEnabled)
RaiseCardGroupNotification(MoveCardModel, MoveCardInputmodel).ConfigureAwait(false);
return Json(MoveCardEntity, JsonRequestBehavior.AllowGet);
}
private async Task RaiseCardGroupNotification(MoveCard moveCard, MoveCardInput moveCardInput)
{
var account = GetPayerAccountsById(moveCard.AccountId.Value);
var currentCardGroupName = Request.Form[Constant.CURRENT_CARD_GROUP_NAME].ToString(System.Globalization.CultureInfo.InvariantCulture);
string targetCardGroupName = moveCardInput.TargetNewCardGroupName;
int[] cardIdList = new[] { moveCard.CardId.Value };
string[] cardPansList = new[] { moveCard.PAN };
var cardList = _cardServiceEx.GetCardListByIds(account, cardIdList, LoggedInUserSession.UserName);
targetCardGroupName = !string.IsNullOrEmpty(cardList.FirstOrDefault().CardGroupName) ? cardList.FirstOrDefault().CardGroupName : string.Empty;
cardPansList = new string[] { cardList.FirstOrDefault().FullPan };
var user = new UserDto
{
UserUuid = LoggedInUserSession.UserName,
DisplayName = LoggedInUserSession.DisplayName,
Locale = LoggedInUserSession.PreferredLanguage,
};
//new card group notification
if (!string.IsNullOrEmpty(moveCardInput.TargetNewCardGroupName))
{
await RaiseNewCardGroupAddedNotification(account, user, moveCardInput.TargetNewCardGroupName).ConfigureAwait(false);
await RaiseCardAddedToCardGroupNotification(account, user, currentCardGroupName, cardPansList, cardList.ToList()).ConfigureAwait(false);
}
else
{
targetCardGroupName = Request.Form[Constant.TARGET_CARD_GROUP_NAME].ToString(System.Globalization.CultureInfo.InvariantCulture);
await RaiseMoveCardGroupNotification(account, user, currentCardGroupName, moveCardInput.TargetCardGroupId.Value, targetCardGroupName, cardPansList, cardList.ToList()).ConfigureAwait(true);
}
}
private async Task RaiseNewCardGroupAddedNotification(AccountDto account, UserDto user, string newCardGroupName)
{
var dto = new RaiseCardGroupsNewNotificationDto
{
CardGroupNameNew = newCardGroupName
};
await _cardGroupNotificationRaiseService.NewAsync(account, user, dto).ConfigureAwait(false);
}
private async Task RaiseCardAddedToCardGroupNotification(AccountDto account, UserDto user, string cardGroupName, string[] cardPans, IList<CardDto> cardDtoList)
{
var dto = new RaiseCardGroupsAddCardsNotificationDto
{
CardGroupName = cardGroupName,
CardPans = cardPans,
CardDtoList = cardDtoList
};
await _cardGroupNotificationRaiseService.AddCardsAsync(account, user, dto).ConfigureAwait(true);
}
if(MoveCardEntity.Error.code==Constant.API\u Error\u code\u SUCCESS)
if(LoggedInUserSession.CustomerConfigPermission.IsNotificationsEnabled)
RaiseCardGroupNotification(MoveCardModel、MoveCardInputmodel)。ConfigureWait(false);
返回Json(MoveCardEntity,JsonRequestBehavior.AllowGet);
}
专用异步任务RaiseCardGroupNotification(MoveCard MoveCard,MoveCardInput MoveCardInput)
{
var account=GetPayerAccountsByd(moveCard.AccountId.Value);
var currentCardGroupName=Request.Form[Constant.CURRENT\u CARD\u GROUP\u NAME].ToString(System.Globalization.CultureInfo.InvariantCulture);
字符串targetCardGroupName=moveCardInput.TargetNewCardGroupName;
int[]carddlist=new[]{moveCard.cardd.Value};
字符串[]cardPansList=new[]{moveCard.PAN};
var cardList=_cardServiceEx.GetCardListByIds(帐户、卡片列表、LoggedInUserSession.UserName);
targetCardGroupName=!string.IsNullOrEmpty(cardList.FirstOrDefault().CardGroupName)?cardList.FirstOrDefault().CardGroupName:string.Empty;
cardPansList=新字符串[]{cardList.FirstOrDefault().FullPan};
var user=new UserDto
{
UserUuid=LoggedInUserSession.UserName,
DisplayName=LoggedInUserSession.DisplayName,
Locale=LoggedInUserSession.PreferredLanguage,
};
//新卡组通知
如果(!string.IsNullOrEmpty(moveCardInput.TargetNewCardGroupName))
{
等待RaiseNewCardGroupAddedNotification(帐户、用户、moveCardInput.TargetNewCardGroupName)。配置等待(false);
等待RaiseCardAddedToCardGroup通知(帐户、用户、currentCardGroupName、cardPansList、cardList.ToList())。配置等待(false);
}
其他的
{
targetCardGroupName=Request.Form[Constant.TARGET\u CARD\u GROUP\u NAME].ToString(System.Globalization.CultureInfo.InvariantCulture);
等待RaiseMoveCardGroupNotification(帐户、用户、currentCardGroupName、moveCardInput.TargetCardGroupId.Value、targetCardGroupName、cardPansList、cardList.ToList())。配置等待(true);
}
}
专用异步任务RaiseNewCardGroupAddedNotification(AccountDto account,UserDto user,string newCardGroupName)
{
var dto=新的RaiseCardGroupsNewNotificationDto
{
CardGroupName新建=新建CardGroupName
};
wait _cardGroupNotificationRaiseService.NewAsync(帐户、用户、dto).configureWait(false);
}
专用异步任务RaiseCardAddedToCardGroupNotification(AccountDto account,UserDto user,string cardGroupName,string[]cardPans,IList CardToList)
{
var dto=新的RaiseCardGroupsAndCardSnotificationDTO
{
CardGroupName=CardGroupName,
万向节=万向节,
CardDtoList=CardDtoList
};
wait_cardGroupNotificationRaiseService.AddCardsAsync(帐户、用户、dto)。配置wait(true);
}
听起来这里的困惑是:
实际上,这两个函数应该同时异步运行
听起来像是要求它们同时运行,但“异步”和“并发”几乎完全不相关。在这种情况下,您可能会通过使用任务进行欺骗。运行在线程池上运行其中一个任务,然后只需等待返回该任务,以便等待两个完成:
var pending=Task.Run(()=>RaiseNewCardGroupAddedNotification(帐户、用户、移动输入.TargetNewCardGroupName));
等待RaiseCardAddedToCardGroup通知(帐户、用户、currentCardGroupName、cardPansList、cardList.ToList())。配置等待(false);
等待等待。配置等待(false);
这基本上是:
- 在线程池上重新开始
- 从当前线程启动B并等待B的完成
- 等待A的完成,这样所有的事情都会被解释清楚
但是,请注意,并非所有代码都是为并发而设计的,其行为完全取决于您的代码。通常,await
的目的是实现异步性,而不需要任何复杂的并发性。听起来这里的混淆是:
实际上,这两个函数应该同时异步运行
听起来像是要求它们同时运行,但“异步”和“并发”几乎完全不相关。在这种情况下,您可能会通过使用任务进行欺骗。运行在线程池上运行其中一个任务,然后只需等待返回该任务,以便等待两个完成:
var pending=Task.Run(()=>RaiseNewCardGroupAddedNotification(帐户、用户、移动输入.TargetNewCardGroupName));
等待RaiseCardAddedToCardGroup通知(帐户、用户、currentCardGroupName、cardPansList、cardList.ToList())。配置等待(false);
等待等待。配置等待(false);
这基本上是:
- 在线程池上重新开始
- 从当前线程启动B并等待B的完成
- 等待A的完成,这样所有的事情都会被解释清楚
但是,请注意,并非所有代码都是为并发而设计的,其行为完全取决于您的代码。通常,await
的目的是实现异步性,而无需任何并发复杂性。第一个操作是否完成(eve)