[英]In C#, if any asynchronous operation in a method makes an entire method async, why would I need more than one await statement in the method?
[英]I need to execute one method multiple time as async in c#
我需要使用wait操作符多次在c#中執行一種方法作為異步方法。 以下是我的代碼
Task.Factory.StartNew(() => SentSMS(null, creation.SenderMobile, messageToSender));
Task.Factory.StartNew(() => SentSMS(null, creation.ReceiverMobile, messageToReciver));
Task.Factory.StartNew(() => SentSMS(null, empMobile, messageToEmp));
private async Task SentSMS(SMSReceived smsrecived, string mobilenumber ,string message)
{
try
{
sent = new SMSSent();
sent.MobileNumber = Util.ParseMobileNo(mobilenumber);
sent.MobileOperator = Util.GetMobileOperator(mobilenumber);
sent.QueryCodeId = 1;
sent.ReplyByTelcoID = 1;
sent.ReplyText = message;
sent.ReplyByTelcoID = 1;
sent.SMSReceivedId = (smsrecived == null ? 0 : smsrecived.ID);
sent.SMSSentDate = DateTime.Now;
sent.Status = 1;
sentBL.Save(sent);
}
catch (Exception)
{
}
}
問題是,當最后一個請求啟動時,它將覆蓋其余2條記錄。 執行此代碼后,我已在數據庫中檢查了它保存一次/相同記錄的次數3次。
不要告訴我我要傳遞三次相同的數據。 所有值都不同。 我需要幫助。 讓我知道我是否缺少一些地方。 我也嘗試了Task.Run()。 我不想使用await。 等待中,結果也一樣
屏幕快照已附加數據庫
不要告訴我我要傳遞三次相同的數據。 所有值都不同。
但是它們不是,因為sent
是一個字段,並且被覆蓋,然后所有值都相同。
使sent
本地創建的方法內部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.