【发布时间】:2019-05-01 15:16:06
【问题描述】:
当我点击停止按钮时,我的计时器仍在倒计时,即使我告诉它停止。
我目前的相关代码:
我在这里命名计时器,因为我还需要访问它们以获得停止/启动所有按钮。
namespace Row_Interface
{
public partial class MainWindow : Window
{
//Declare the timers here, so the stop all button can access them as well
DispatcherTimer motorTimer_1 = new DispatcherTimer();
TimeSpan motorCycleTime_1 = TimeSpan.FromSeconds(0);
当我点击 on 按钮时,IndividualTestStart 方法被调用并传递了相关参数:
public void motorOnBtn_1_Click(object sender, RoutedEventArgs e)
{
IndividualTestStart(motorOnBtn_1, motorOffBtn_1, motorTimer_1, motorCycleTime_1, timeUntilmotorCycle_1, motorTestCycles_1);
}
当我单击关闭按钮时,我想停止该计时器,以便循环永远不会结束:
private void motorOffBtn_1_Click(object sender, RoutedEventArgs e)
{
motorTimer_1.Stop();
motorOnBtn_1.IsEnabled = true; //Enables the start test button
motorOffBtn_1.IsEnabled = false; //Disables the stop test button
}
当我单击开始时调用它。我最终会有类似的停止按钮,但我一步一步地采取措施:
private void IndividualTestStart(Button startButton, Button stopButton, DispatcherTimer dispatcherTimer, TimeSpan timeSpan, TextBox timeRemaining, TextBox cycleCount)
{
stopButton.IsEnabled = true; //Enables the stop button
//Set the time to run. This will be set from the database eventually.
timeSpan = TimeSpan.FromSeconds(10);
//Set up the new timer. Updated every second.
dispatcherTimer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
{
timeRemaining.Text = timeSpan.ToString("c"); //Sets the text in the textbox to the time remaining in the timer
startButton.IsEnabled = false; //Disables the start test button once the test is started
if (timeSpan == TimeSpan.Zero) //Checks to seee if the time has run out
{
dispatcherTimer.Stop(); //Stops the timer once the time has run out
startButton.IsEnabled = true; //Enables the start test button
int initialCycleCount = 0;
initialCycleCount++;
cycleCount.Text = initialCycleCount.ToString();
stopButton.IsEnabled = false;//Disables the stop button
}
timeSpan = timeSpan.Add(TimeSpan.FromSeconds(-1)); //Subtracts one second each time the timer "ticks"
}, Application.Current.Dispatcher); //runs within the UI thread
dispatcherTimer.Start(); //Starts the timer
}
}
当我单击停止按钮时,我希望文本框中的计时器停止倒计时。但是,它只是不断滴答作响。当我单击停止时,开始按钮被重新启用,所以我知道它正在触发事件处理程序中的代码。但它并没有停止计时器。
现在没有开始新的计时器。 新代码:
public void motorOnBtn_1_Click(object sender, RoutedEventArgs e)
{
IndividualTestStart(motorOnBtn_1, motorOffBtn_1, motorTimer_1, motorCycleTime_1, timeUntilmotorCycle_1, motorTestCycles_1);
}
private void IndividualTestStart(Button startButton, Button stopButton, DispatcherTimer dispatcherTimer, TimeSpan timeSpan, TextBox timeRemaining, TextBox cycleCount)
{
stopButton.IsEnabled = true; //Enables the stop button
//Set the time to run. This will be set from the database eventually.
timeSpan = TimeSpan.FromSeconds(10);
{
timeRemaining.Text = timeSpan.ToString("c"); //Sets the text in the textbox to the time remaining in the timer
startButton.IsEnabled = false; //Disables the start test button once the test is started
if (timeSpan == TimeSpan.Zero) //Checks to seee if the time has run out
{
dispatcherTimer.Stop(); //Stops the timer once the time has run out
startButton.IsEnabled = true; //Enables the start test button
int initialCycleCount = 0;
initialCycleCount++;
cycleCount.Text = initialCycleCount.ToString();
stopButton.IsEnabled = false;//Disables the stop button
}
timeSpan = timeSpan.Add(TimeSpan.FromSeconds(-1)); //Subtracts one second each time the timer "ticks"
}; //runs within the UI thread
dispatcherTimer.Start(); //Starts the timer
}
【问题讨论】:
-
您将
motorTimer_1作为参数dispatcherTimer传递给IndividualTestStart()。然后用新创建的DispatcherTimer替换参数的值,然后初始化并启动它。motorTimer_1永远不会启动,也永远不会引用实际运行的DispatcherTimer。 -
啊,有道理。但是,我删除了该参数值的替换,现在计时器根本不会滴答作响。现在正在努力。
-
停下来想一想。您现在如何初始化 DispatcherTimer
motorTimer_1?你给它一个间隔、一个优先级、一个委托、一个调度程序吗?在哪里?如何?您是否像以前一样创建一个新的 DispatcherTimer,但只是将其丢弃而不是将其分配给参数?让我们看看新代码。 -
```` timeSpan = TimeSpan.FromSeconds(10); { timeRemaining.Text = timeSpan.ToString("c"); //将文本框中的文本设置为计时器中的剩余时间 if (timeSpan == TimeSpan.Zero) { dispatcherTimer.Stop(); } timeSpan = timeSpan.Add(TimeSpan.FromSeconds(-1)); }; dispatcherTimer.Interval = new TimeSpan(0, 0, 1); dispatcherTimer.Start(); //启动定时器 } ````
-
请将其添加到问题中,以便我阅读。
标签: c# wpf button dispatchertimer