0

I have Written this function to update timer time in every single of second.

DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromSeconds(1.0);

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
            ApplicationVariables.ServerDateTime = CurrentDT = CurrentDT.AddSeconds(1);     
}

But if my application hanged for some time then application timer stop at that time and after responding application timer start and started from last time rather than adding application hanged time with last time to show actual time. Please suggest me how to keep timer on even if application get hang.

I am using BackgroundWorker object to maintain application hang state.

It normally hang when my application trying to initialize object of fujitsu reader.

Please find attachment to more details.

enter image description here

4

1 回答 1

0

我建议使用非调度程序计时器来执行您的任务并在必要时通过调用来更新 UI。

public System.Threading.Timer MyTimer { get; set; }

this.MyTimer = new System.Threading.Timer(new TimerCallback(MyTimer_CallBack), null, 1000, 1000);

void MyTimer_CallBack(object State)
{
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (System.Threading.ThreadStart)delegate()
{
  ApplicationVariables.ServerDateTime = CurrentDT = CurrentDT.AddSeconds(1); 
});
}
于 2012-12-10T15:03:41.283 回答