Frage

Ich schaffe timed Online-Test in C # asp.net. Ich habe es geschaffen, um die lokale Maschinenzeit verwenden. Aber im Fall von Datenbank Trennung oder System hängt, muss der Countdown-Timer ab dem Zeitpunkt beginnen, an dem die Maschine aufgehängt hat oder unerwartete Datenbank Zu. Zum Beispiel ist die Beantwortung der Benutzer die Fragen für 10 Minuten und unerwartet gehängt System. Nach der Wiederherstellung muss der Countdown von 10 Minuten starten. Kann mir jemand helfen mit Codierung in C # bitte?

Ich habe den folgenden Code, aber es ist nicht sinnvoll in dem obigen Szenario verwendet.

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");
}
War es hilfreich?

Lösung

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>

Andere Tipps

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

You can use windows registery to store the counter.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top