タイマーイベントを別のスレッドとして呼び出そうとしているこの関数がありますが、ページのいずれかのボタンをクリックするか、asp.netページで何かを実行すると、タイマーが1秒間停止します。
タイマーは毎秒実行されるべきであり、UIで停止するべきではないため、ページ内の別のコントロールの影響を受けずに並列で実行する方法を支援してください。
Thread obj = new Thread(new ThreadStart(timer));
obj.Start();
obj.IsBackground = true;
protected void timer()
{
Timer1.Interval = 1000;
Timer1.Tick += new EventHandler<EventArgs>(Timer1_Tick);
Timer1.Enabled = true;
}
public void TimerProc(object state)
{
fromTime = DateTime.Parse(FromTimeTextBox1.Text);
tillTime = DateTime.Parse(TillTimeTextBox1.Text);
DateTime currDateTime = System.DateTime.Now;
TimeSpan interval = tillTime - currDateTime;
if (tillTime <= currDateTime)
{
ExamOverPanel1.Visible = true;
QuestionPanel.Visible = false;
ListBox2.Visible = false;
StatusPanel1.Visible = false;
VisitLaterLabel.Visible = false;
}
else
{
minLabel.Text = string.Format("{0:00}:{1:00}:{2:00}", (int)interval.TotalHours, interval.Minutes, interval.Seconds);
}
}