izmo

izmo

  • NA
  • 10
  • 0

Regarding on timer!

Apr 27 2005 12:15 AM
my program contains a timer that runs after a button is pressed to perform certain action. the situation is as following: By default the timer is set to False. After OK button is pressed, the timer is set to True (starts to run). I have use 'elapsed' to take the elapsed time as second to compare against value inside Session["Duration"] that i have converted into integer value. If elapsed's second is equal to value of Session["Duration"] then the status set to Invalid which will be saved into database (action is performed) and the timer is stopped. But when i run the program...the timer never runs......i feel that the method i use below is not correct...what i want to do is that a timer can run the following code while my program can carry out other actions at the same time. but now my timer seems not working....any comments or ways of doing this??....and what to do in order to implement it as a backgroud thread for the following......thanks.....it's urgent! ... private DateTime lastTime; private void CmdOK_Click(object sender, System.EventArgs e) { lastTime = DateTime.Parse(Session["Time"].ToString()); Session["Duration"] = txtDuration.Text; tmrMoney.Enabled = true; } ... private void tmrMoney_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { DateTime nowTime = DateTime.Now; TimeSpan elapsed = nowTime - lastTime; string meter = TxtMeter.Text; if (elapsed.Seconds = int.Parse(Session["Duration"].ToString())) { SqlCommand myCommand = new SqlCommand("UPDATE PaymentRecord SET Status = 'Invalid' WHERE Meter_Number = '" + meter + "'", myConnection); myConnection.Open(); myCommand.ExecuteNonQuery(); tmrMoney.Enabled = false; } lastTime = nowTime; } ... The timer interval is set to 1000 and tmrMoney_Elapsed is set to Elapsed event...I just really cannot make the timer work..so hope you guys can help...THANX...

Answers (1)