I am creating timed online test in C# asp.net. I have created it using the local machine time. But in the case of Database disconnection or system hang, The countdown timer must start from the time at which the machine has hanged or unexpected database disconnectivity. For example the user is answering the Questions for 10 mins and unexpectedly system hanged. After recovery the countdown must start from 10 mins. Can anyone please help me with coding in C#?
I have used the following code, But its not useful in the above scenario.
int totalTime = (Convert.ToInt32(ViewState["totaltime"])) * 60;
DateTime startTime = (DateTime)ViewState["startTime"];
TimeSpan elaspedTime = DateTime.Now.Subtract(startTime);
Literal1.Text = elaspedTime.Hours.ToString() + "h" + ":" + elaspedTime.Minutes.ToString() +
"m" + ":" + elaspedTime.Seconds.ToString() + "s";
int finish = Convert.ToInt32(elaspedTime.TotalSeconds);
int remaingsec = (totalTime - finish);
TimeSpan remainingtime = TimeSpan.FromSeconds(remaingsec);
string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
remainingtime.Hours,
remainingtime.Minutes,
remainingtime.Seconds,
remainingtime.Milliseconds);
LIteral2.Text = answer;
if (totalTime == finish)
{
lnkFinish_Click(sender, e);
Response.Redirect(@"~/Error.aspx");
}
The Timer class in C# represents a Timer control that executes a code block at a specified interval of time repeatedly. For example, backing up a folder every 10 minutes, or writing to a log file every second. The method that needs to be executed is placed inside the event of the timer.
My opinion is to use javascript for the countdown (embedded as well) and to pass your DB data with json, when DB cause disconnection you pass some data to javascript and with a control you proced to the countdown.
Download the following javascript file and put into your web application directory: http://scripts.hashemian.com/js/countdown.js
And than use something like this:
<script type="text/javascript">
TargetDate = "12/25/207 12:01 AM";
BackColor = "red";
ForeColor = "white";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is here!";
</script>
i think its better to take a field in table and set total time for question in it and update that field on every 15/30 seconds (as per your required frequency) and on every update substract the value 15/30 second from total time. and do this till the field has value > 0.
for this you can take help of timer (javascript timer) and ajax. to send request to server to update that field.
with ajax i think its better to create a webservice (which will update value of that field) and call it using ajax on basis of javascript timer. well you can also use ajax timer.
javascript timer reference:
http://dotnetacademy.blogspot.com/2010/09/timer-in-javascript.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With