逻辑说:用户1通过服务器1向用户2发送消息。服务器1将其记录在DB(mysql)中并将消息发送到服务器2以发送给用户2.服务器1响应用户1该消息具有已经送走了。在后端(异步)服务器1不断请求服务器2传递消息,在传递时,更新数据库并将传递状态设置为已传递或失败(在重新尝试几个小时后)。
但我认为这段代码不起作用。因为数据库永远不会更新。
SendMessage.aspx
protected void btnSend_Click(object sender, EventArgs e)
{
global g = new global();
g.CallUpdateSMSStatus(cmdSMS.LastInsertedId.ToString(), msgID);
}
global.cs
public async void CallUpdateSMSStatus(string strMsgID, string strMsgID)
{
await updateMessageStatus(strMsgID, strMsgIDServer);
}
public async Task updateMessageStatus(string strMsgID, string strMsgID)
{
global g = new global();
int timer = 10000;
int intStatus = 2;
// 2 - means Message Sent - Any other number finishes delivery request
while (intStatus == 2)
{
await Task.Run(() =>
{
intStatus = g.checkSMSStatus(strMsgIDServer);
//checkSMSStatus REQUEST SERVER 2 for status of message
if (intStatus != 2)
{
//Update Status in the Database
}
else
{
if (timer < 3600000)
{
timer *= 10;
}
Thread.Sleep(timer);
}
});
}
}
答案 0 :(得分:0)
我认为这就是你想要的:
public async Task updateMessageStatus(string strMsgID, string strMsgIDServer, int timeToNext)
{
await Task.Run(() =>
{
global g = new global();
int intStatus = g.checkSMSStatus(strMsgIDServer);
//checkSMSStatus REQUEST SERVER 2 for status of message
if (intStatus != 2)
{
//Update Status in the Database
}
else
if (timeToNext < 3600000)
Task.Delay(timeToNext).ContinueWith((t) => updateMessageStatus(strMsgID, strMsgIDServer, timeToNext * 10));
});
}