V A Thomas

V A Thomas

  • 1.7k
  • 48
  • 660

Can't get async and await working

Jun 23 2017 10:48 AM

Logic says: User 1 sends a message to User 2 via Server 1. Server 1 records it in DB (mysql) and sends the message to Server 2 to be sent to User 2. Server 1 responds to User 1 that message has been sent. While at the back-end (asynchonously) Server 1 keeps requesting Server 2 for the delivery of the message, when delivered, updates the database and set delivery status as delivered or failed (after few hours of re-trying).

But I don't think this code is working. Because database never gets updated.

SendMessage.aspx

  1. protected void btnSend_Click(object sender, EventArgs e)  
  2. {  
  3.     global g = new global();  
  4.     g.CallUpdateSMSStatus(cmdSMS.LastInsertedId.ToString(), msgID);  

 

global.cs

  1. public async void CallUpdateSMSStatus(string strMsgID, string strMsgID)  
  2.     {  
  3.         await updateMessageStatus(strMsgID, strMsgIDServer);  
  4.     }  
  5.   
  6.     public async Task updateMessageStatus(string strMsgID, string strMsgID)  
  7.     {  
  8.         global g = new global();  
  9.         int timer = 10000;  
  10.         int intStatus = 2;  
  11.         // 2 - means Message Sent - Any other number finishes delivery request  
  12.         while (intStatus == 2)  
  13.         {  
  14.             await Task.Run(() =>  
  15.             {  
  16.                 intStatus = g.checkSMSStatus(strMsgIDServer);  
  17.                 //checkSMSStatus REQUEST SERVER 2 for status of message  
  18.                 if (intStatus != 2)  
  19.                 {  
  20.                     //Update Status in the Database   
  21.                 }  
  22.                 else  
  23.                 {  
  24.                     if (timer < 3600000)  
  25.                     {  
  26.                         timer *= 10;  
  27.                     }  
  28.                     Thread.Sleep(timer);  
  29.   
  30.                 }  
  31.             });  
  32.         }  
  33.     }
 

Answers (2)