I need to execute one method multiple time as async in c# with using the wait operator. following is my code
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)
{
}
}
The problem is , when the last request started , it override rest of 2 records. After execution of this code, i have checked in database it save one/same record 3 times.
do not tell me i am passing same data thrice. All the values are different. I need help. Let me know if i am lacking some where. I have also tried Task.Run(). I do not want to use await . In await , results are also same
Screenshot has attached of database
sentpoint to? I don't see it declared anywhere in the function so I assume it's a common variable inside the class? If so, it will in fact be replaced each time the function is invoked. You probably might want to declare it like this:var sent = new SMSSent();and then return it at the end.sent.