Have you considered Ajax? are you storing the last updated time on manually setting the flag? You can use Ajax request to simultaneously run using jquery Set interval. which will trigger the ajax request every 2 minutes. Find the last time it was updated, if that is passed 20 minutes then remove one from the database, your return would be the new number and jquery can update that number for you.
Quite a simple process actually but need more detail on the underlying data.
Here is how I can see it working from your question
In Controller
public ActionResult ajaxUpdate()
{
//open connection
dbcontext db = new dbcontext();
db.Connection.Open();
// get the last updated record in the database.
var entry = db.Entry.OrderByDecending(m=> m.LastUpdatedDate).FirstOrDefault();
//clean up
db.Connection.Close();
db.Dispose();
//return -1 as error
if(entry == null){
return Json(-1,JsonRequestBehavior.AllowGet);
}
// get current number of people in queue
Int32 numberOfPeople = entry.QueueNumber;
TimeSpan span = DateTime.Now.Subtract(entry.LastUpdatedDate);
if(span.Minutes >= 20){
// if 20 mins have passed assume a person has been completed since manual update
numberOfPeople--;
}
//this returns a number, alternatively you can return a Partial
return Json(numberOfPeople, JsonRequestBehavior.AllowGet);
}
Jquery and Ajax
$(document).ready(function () {
// run function every x minutes
setInterval(function () {
UpdateQueue();
}, 100000);
});
function UpdateQueue() {
$.ajax({
cache: true,
type: 'POST',
url: "/ControllerName/ajaxUpdate",
async: false,
dataType: "json",
success: function (result) {
// on success result will be the number returned
// -1 is error
if (result == -1) {
return;
}
// check the -- didn't return a negative
if (result < 0) {
result = 0;
}
//find your element in the HTML to update
$('#NumberElement').text().replaceWith(result);
}
});
}
You must ensure you include your jquery libraries before you include this code or you will have Jquery not defined.