我尝试每10秒在10分钟后在文本文件中写入一些值。 我的代码看起来像这样。
private void button12_Click(object sender, RoutedEventArgs e)
{
DispatcherTimer tPOI = new DispatcherTimer();
tPOI.Tick += new System.EventHandler(dispatcherTimer_TickPOI);
tPOI.Interval = new TimeSpan(0, 0, 10);
tPOI.Start();
MessageBox.Show("recording POI");
}
private void dispatcherTimer_TickPOI(object sender, EventArgs e)
{
using (StreamWriter file = new StreamWriter(@textBox36.Text, true))
{
file.WriteLine("pos POI");
file.WriteLine(textBox40.Text);
file.WriteLine(textBox41.Text);
file.WriteLine(textBox42.Text);
file.WriteLine(textBox46.Text);
}
}
但是现在我不知道如何在10分钟后停止计时器。有人对我有提示吗?
答案 0 :(得分:0)
您可以执行以下操作。跟踪StartTime
并检查它是否超过10分钟。
private DateTime _startTime; //track the start time
private DispatcherTimer tPOI;
private void button12_Click(object sender, RoutedEventArgs e)
{
tPOI = new DispatcherTimer();
tPOI.Tick += new System.EventHandler(dispatcherTimer_TickPOI);
tPOI.Interval = new TimeSpan(0, 0, 10);
_startTime = DateTime.Now; //assign start time when it actually starts
tPOI.Start();
MessageBox.Show("recording POI");
}
private void dispatcherTimer_TickPOI(object sender, EventArgs e)
{
if (_startTime.AddMinutes(10) < DateTime.Now) //check if it has crossed 10 mins
{
tPOI.Stop();
}
else
{
using(StreamWriter file = new StreamWriter(@textBox36.Text, true))
{
file.WriteLine("pos POI");
file.WriteLine(textBox40.Text);
file.WriteLine(textBox41.Text);
file.WriteLine(textBox42.Text);
file.WriteLine(textBox46.Text);
}
}
}