M_K*_*M_K 2 c# windows-phone-7
我正在使用DispatchTimer每秒做一些事情,我怎样才能让它做一段时间,比如1分钟或2分钟?我需要花药嵌套的DispatcherTimer吗?
我的代码如下
System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
private void StartButton_Click(object sender, RoutedEventArgs e)
{
TimeSpan interval;
interval = (TimeSpan)intervalPicker.Value;
dt.Interval = interval;
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
}
void dt_Tick(object sender, EventArgs e)
{
//Do Something
}
Run Code Online (Sandbox Code Playgroud)
如果你能帮助我,我将不胜感激.
使用Stopwatch课程:
Stopwatch sw = new Stopwatch();
private void StartButton_Click(object sender, RoutedEventArgs e)
{
sw.Start();
...
}
void dt_Tick(object sender, EventArgs e)
{
// stops the timer after 66 seconds
if(sw.ElapsedMilliseconds/1000 > 66)
{
dt.Stop();
sw.Reset();
}
}
Run Code Online (Sandbox Code Playgroud)