Stopping DispatcherTimer with a button click - c#

When I click my stop button, my timer is still counting down, even though I tell it to stop.
My current relevant code:
I'm naming the timers here, as I need to access them for a stop/start all button as well.
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);
When I click the on button, the IndividualTestStart method is called & passed the relevant parameters:
public void motorOnBtn_1_Click(object sender, RoutedEventArgs e)
{
IndividualTestStart(motorOnBtn_1, motorOffBtn_1, motorTimer_1, motorCycleTime_1, timeUntilmotorCycle_1, motorTestCycles_1);
}
When I click the off button, I'm wanting to stop that timer so the cycle never finishes:
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
}
This is called when I click start. I'll eventually have something similar for the stop button, but I'm taking things one step at a time:
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
}
}
When I click the stop button, I expect the timer in the textbox to stop counting down. However, it just keeps on ticking. When I click stop, the start button is re-enabled, so I know that it's triggering the code in the event handler. But it isn't stopping the timer.
Not starting a new timer now.
New code:
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
}

The problem in your code is that you initialize motorTimer_1 with a DispatcherTimer that doesn't do anything, then you pass motorTimer_1 in as the dispatcherTimer parameter, and then you replace the value of the parameter with a newly created, different DispatcherTimer.
The new timer works fine, but when you call stop on motorTimer_1, nothing happens, because that's not the one that's running. You could simply assign the new DispatcherTimer directly to motorTimer_1 in IndividualTestStart(), but you've gone to great trouble to parameterize everything in IndividualTestStart() so it can work with different DispatcherTimers.
Instead, here's what we'll do: There is no reason to pass in a DispatcherTimer. IndividualTestStart() must create the DispatcherTimer in order to initialize it. OK, let's run with that. It will create a new one and return it.
private DispatcherTimer IndividualTestStart(Button startButton, Button stopButton,
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.
var 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
return dispatcherTimer;
}
public void motorOnBtn_1_Click(object sender, RoutedEventArgs e)
{
if (motorTimer_1 == null)
{
// Create/initialize a new timer and assign it to motorTimer_1
motorTimer_1 = IndividualTestStart(motorOnBtn_1, motorOffBtn_1,
motorCycleTime_1, timeUntilmotorCycle_1, motorTestCycles_1);
}
else
{
// It's already there, just start it.
motorTimer_1.Start();
}
}
Since this is WPF, you'll want to write a viewmodel class TimerThing (think of a better name) that owns a DispatcherTimer, two commands to start it and stop it, and a public bool property that indicates whether it's running or not. IndividualTestStart() should be a method of that class. The parent viewmodel will have have an ObservableCollection<TimerThing> containing an arbitrary number of TimerThings, which will be displayed in an ItemsControl with an ItemTemplate that creates buttons bound to the Start and Stop commands. The above code will look very different, since none of the C# code will know anything about buttons: Instead, the buttons in the item template XAML will be enabled/disabled by bindings.

Related

C# - Update UI on Timer tick while delaying based on Timer value

I want my WPF application to display a 3-second countdown before displaying an image, using a timer to display the "3, 2, 1" count based on the elapsed three seconds (meaning use the timer to update the UI) while waiting to display the image until the three seconds have elapsed (meaning have the method wait).
In pseudocode, I am trying to do the following:
3 is displayed, then one second elapses
2 is displayed, then one second elapses
1 is displayed, then one second elapses
The image is displayed
I am trying to use a System.Timers.Timer, such as:
...
public int Countdown = 3;
...
private void UpdateCountdown(object source, EventArgs e)
{
Countdown--;
}
public void DoStuff()
{
Timer timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += UpdateCountdown;
timer.Start();
while (Countdown > 0)
{
// do not act until the 3 seconds have elapsed
}
// now that 3 seconds have elapsed...
ShowImage();
}
and in my xaml, I am binding the Countdown value to a TextBlock:
<TextBlock Name="TbCountdown" Text="{Binding Path=Countdown}"/>
I have tried a few different things, such as trying Dispatcher.Invoke. My problem is that one of the two following things always happens:
The UI gets blocked and does not show 3 turn into 2, etc.
The UI shows the 3, 2, 1 correctly but the ShowImage() method does not wait for the 3 seconds, it just happens instantly.
Any advice?
Don't busy spin on the Countdown value in DoStuff, rather put the test and ShowImage() in the UpdateCountdown handler.
private void UpdateCountdown(object source, EventArgs e)
{
if (--Countdown == 0)
ShowImage();
}
public void DoStuff()
{
Timer timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += UpdateCountdown;
timer.Start();
}

New DispatcherTimer gets created alongside old one, only new one should run

I ran into a problem the other day. I have found out why it's happening, but I've never had a run in with such a problem so I don't know how to solve it.
I have an application where in the DashboardView (the main view) a DispatcherTimer is started in the DashboardViewModel. When the Timer ticks, we get the data from the database, this list is databound between the View and the ViewModel. When there is new data that caused the database to change, a sound will play.
The user can go to other Views. When the user goes back to the DashboardView, the DashboardViewModel is again created and so is the DispatcherTimer.
Now there are 2 Timers and they both fire the Tick event, creating a confusing scenario for the user.
This is my observation of what happens in the application right now:
My Timer ticks every minute. When I start the application, DashboardView #1 opens. DashboardViewModel #1 starts and so does DispatcherTimer #1.
I switch to a different view, and make an update to the data (a new email) so when the Timer ticks, the list in the DashboardView will change and a sound is played.
When Timer #1 is at 30 seconds, I switch to the DashboardView, which is newly created thus creating View&ViewModel&Timer #2.
After 1 minute, Timer #1 ticks, there is new data so it updates the DB and plays a sound, yet the list in the View doesn't update.
I think that this is because View #2 is showing over #1. I know because otherwise I would see an overlay saying it's refreshing.
View #2 is databound to ViewModel #2. Timer #1 updated ViewModel #1, so the changes won't show as we can't see View #1 as it's replaced/overlapped by View #2.
After 1 min 30 seconds, Timer #2 ticks, gets the data from the DB, doesn't play a sound as the DB was already brought up-to-date by Timer #1, and shows the data in the new state.
(I hope that made sense)
So, TLDR: There are 2 Timers running while only 1 should be active (the newest one, I think).
How can I achieve this?
Here's (part of) the DashboardViewModel as I have it now:
namespace QRM.ViewModel
{
class DashboardListViewModel : INotifyPropertyChanged
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
DBServer dbServer = new DBServer();
#region Constructor
public DashboardListViewModel()
{
log.Info("Dashboard Initializing - Starting...");
MyObservableCollection<View_server> listDashboard = new MyObservableCollection<View_server>();
ListDashboard = dbServer.ReadDashboard();
listBoxCommand = new RelayCommand(() => SelectionHasChanged());
// Refresh to get all new emails, errors, etc.
GetListDashboard();
IsRefreshing = Visibility.Collapsed;
// Make a timer to renew the data in the Dashboard automatically.
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = Properties.Settings.Default.Timer_interval; // hours, minutes, seconds.
timer.Start();
//Receive the Notification sent after DashboardDetailsViewModel has handled the button commands, and call a respond method for the List.
App.Messenger.Register("RefreshServers", (Action)(() => GetListDashboard()));
App.Messenger.Register("ClearSelection", (Action)(() => SelectedServer = null));
App.Messenger.Register("ErrorSolved", (Action)(() => KeepSelection(selectedServer)));
App.Messenger.Register("WarningSound", (Action)(() => HasNewError = true));
log.Info("Dashboard Initializing - Done.");
}
#endregion
#region Get list dashboard
private void GetListDashboard()
{
HasNewError = false;
log.Info("Dashboard - Checking for Email...");
// The old Outlook class and methods
//EmailManager checkMail = new EmailManager();
//checkMail.GetEmail();
// First, check for mail.
IMAPManager checkMail = new IMAPManager();
checkMail.GetEmail();
log.Info("Dashboard - Checking for linked Errors...");
// Check if the emails have Errors linked to them. If not, add the Error from the Email to the DB
ErrorManager checkError = new ErrorManager();
checkError.GetNewErrors();
log.Info("Dashboard List - Starting...");
// Load the dashboard.
ListDashboard = dbServer.ReadDashboard();
System.Diagnostics.Debug.WriteLine("REFRESHED THE DASHBOARD");
log.Info("Dashboard List - Done.");
}
private void KeepSelection(View_server keepSelection)
{
GetListDashboard();
SelectedServer = keepSelection;
SelectionHasChanged();
}
#endregion
#region Timer
//This method runs every time the timer ticks.
private async void timer_Tick(object sender, EventArgs e)
{
log.Info("Dashboard - Refreshing...");
System.Diagnostics.Debug.WriteLine(">>Timer tick");
IsRefreshing = Visibility.Visible;
// To make sure the overlay is visible to the user, let it be on screen for at least a second (2x half a second)
await Task.Delay(500);
if (selectedServer != null)
{
KeepSelection(selectedServer);
}
else
{
GetListDashboard();
}
// 2nd half second.
await Task.Delay(500);
IsRefreshing = Visibility.Collapsed;
if (hasNewError == true)
{
System.Diagnostics.Debug.WriteLine("List has new error");
PlayWarningSound();
HasNewError = false;
}
else
{
System.Diagnostics.Debug.WriteLine("List has no new error");
HasNewError = false;
}
System.Diagnostics.Debug.WriteLine(">>End timer");
log.Info("Dashboard - Refreshed.");
}
#endregion
}
}
There are a few issues going on here. Let's start with the most basic first:
Cleanup
When the DashboardListViewModel is disposed of or closed, you need to unwire your DispatcherTimer.Tick event handler, invoke .Stop() and then call .Finalize(). MSDN. This will ensure that your System.Windows.Threading.DispatcherTimer is properly cleaned up.
Async / Await & Event Handlers
Also, the DispatcherTimer.Tick event handler is defined as async void. This is the incorrect usage of the async keyword. Instead use this:
private void timer_Tick(object sender, EventArgs e)
{
log.Info("Dashboard - Refreshing...");
System.Diagnostics.Debug.WriteLine(">>Timer tick");
IsRefreshing = Visibility.Visible;
// To make sure the overlay is visible to the user, let it be on screen for at least a second (2x half a second)
Thread.Sleep(500);
if (selectedServer != null)
{
KeepSelection(selectedServer);
}
else
{
GetListDashboard();
}
// 2nd half second.
Thread.Sleep(500);
IsRefreshing = Visibility.Collapsed;
if (hasNewError == true)
{
System.Diagnostics.Debug.WriteLine("List has new error");
PlayWarningSound();
HasNewError = false;
}
else
{
System.Diagnostics.Debug.WriteLine("List has no new error");
HasNewError = false;
}
System.Diagnostics.Debug.WriteLine(">>End timer");
log.Info("Dashboard - Refreshed.");
}
I usually never advise using Thread.Sleep but since you're already in the context of a Threading Timer this makes sense.
One last concern
Are you certain that the App.Messenger.Register can be invoked multiple times, as it occurs every time your view model is instantiated? I would have imagined this would be something you'd only ever want to do once, in static context.

How to capture a time interval while waiting for user input?

I have Form with a Timer1, and it set to 10Sec.
There is a KeyDown event - when the user press "Enter", I would like to save in "ans" the time duration that past in the 10S interval before it ends.
For Example: If i starting the timer1 now, and after 3Sec, I'm pressing Enter, ans = 3. and if I didnt press any key, ans will be equal to 10.
I have this code:
if (e.KeyCode == Keys.Enter)
{
ResponseTimeList.Add(timer1.Interval);
}
*ResponseTimeList is:
public List<double> ResponseTimeList = new List<double>();
How can i improve it?
Thanks.
Well, to start out with, Timer is not what you want to use. The timer class is designed to fire off events at a pre-defined interval of time; for example, you might use a timer to update a text box on a form every 10 seconds.
Instead, what you wish to do is use a stopwatch (System.Diagnostics.Stopwatch). Call Stopwatch.Start() whenever you want to start timing. When the user presses enter, simply call Stopwatch.Stop() and then get the time interval that has elapsed in seconds.
Finally, for the 10-second logic, you will need to use something like this (a conditional evaluation):
var timeToDisplay = Stopwatch.ElapsedMilliseconds > 10000 ? 10 : Stopwatch.ElapsedMilliseconds/1000
You can use Tick event of Timer.
bool isPressed = false;
Timer timer1 = new Timer() { Interval = 10000};
timer1.Tick += (s, e) =>
{
if (!isPressed)
ResponseTimeList.Add(timer1.Interval);
isPressed = false;
};
And when key pressed:
if (e.KeyCode == Keys.Enter)
{
ResponseTimeList.Add(timer1.Interval);
isPressed = true;
}

Timer C# Stop() doesn't really stop

I have a form which includes a textbox showing the time passing with an interval of 1 second up till 2 minutes. It is working ok. However I need the timer to stop when a round of game is over and start again when a button from another form is clicked. So I have _timer.Stop() in the timer_tick event handler and _timer.Start() in the button click event handler of Form 2.
My problem is that the timer then starts from how much time passes until I press the button on the other form and not from 0:00 again. Any Ideas?
This is the code regarding the timer:
private DateTime _myDateTime;
private System.Windows.Forms.Timer _timer;
private DateTime newDate;
public Tournament()
{
_timer = new System.Windows.Forms.Timer();
_timer.Interval = 1000;
_timer.Tick += Timer_Tick;
_myDateTime = DateTime.Now;
newDate = new DateTime();
newDate = newDate.AddMinutes(2.00);
_timer.Start();
InitializeComponent();
}
void Timer_Tick(object sender, EventArgs e)
{
var diff = DateTime.Now.Subtract(_myDateTime);
this.textBox1.Text = diff.ToString(#"mm\:ss");
DateTime dt = Convert.ToDateTime(diff.ToString());
if (newDate.Minute == dt.Minute)
{
_timer.Stop();
_myDateTime = DateTime.Now;
displayPointsOrResults();
this.textBox1.Text = diff.ToString(#"mm\:ss");
}
}
In displayPointsOrResults() I am creating instance of another form Points() so in Points form I have button 1 and I have this in its event handler apart other things which does not concern the timer: tournament.Timer1.Start();
Where I am getting the timer from the tournament class and starting it again.
What am I doing wrong?
My problem is that the timer then starts from how much time passes until I press the button on the other form and not from 0:00 again.
Well yes, it would - you're resetting _myDateTime when you stop the timer:
_timer.Stop();
_myDateTime = DateTime.Now;
You should move that second line to the point where you restart it.
Note that your title is incorrect - the timer really is stopping, in that it's not ticking any more. The timer isn't actually performing the timing, if you see what I mean - it's just a way of invoking an event handler regularly. The problem is within the event handler itself.
Ah the global vars, blessing and curse together.
Do you have resetted the _myDateTime to DateTime.Now, when you press the button?
When you stop the timer and restart it, should set the datetime to now.
_myDateTime = DateTime.Now;

How do I add a delay after a count down timer

I am using a DispatcherTimer to perform a count down before triggering the release on a camera. The UpdateCountdown method is used to change the image displayed to the user before the camera fires. Rather than having the TakePicture method execute immediately, I would like have a slight delay after the counter reaches zero and the last image is displayed.
The code shown below results in the pause occurring at the _countdown = 1 point. While the final image displays and TakePicture() fires almost simultaneously (I think TakePicture happens first).
_countdownTimer = new DispatcherTimer();
_countdownTimer.Interval = TimeSpan.FromSeconds(1);
_countdownTimer.Tick += new EventHandler(delegate(object s, EventArgs a)
{ UpdateCountdown(); } );
_countdownTimer.Tick += new EventHandler(delegate(object s, EventArgs a)
{if (_countdown == _countdownMax)
{
System.Threading.Thread.Sleep(2000); // Slight delay before taking picture
Camera.TakePicture();
} });
}
public void StartCountdown()
{
if (doCount)
{
doCount = false;
UpdateCountdown();
_countdownTimer.Start();
}
}
private void UpdateCountdown()
{
_countdown--;
CountdownImage = _countDownImages[_countdown]; // Data bound to Image Control
if (_countdown == 0)
{
_countdown = _countdownMax;
_countdownTimer.Stop();
doCount = true;
}
What am I not taking into account with my timing?
The UI does not update immediately when you change control properties - it only updates when the thread becomes idle (that is, after all your event handlers finish executing).
Thread.Sleep blocks the thread, the event handlers don't finish executing and UI isn't redrawn.
You have to either use another timer (start a new timer on the last tick of the existing timer and call TakePicture on teh new timer's tick) or, even better, use the last tick of the existing timer - update UI when (_countdown <= _countdownMax), take picture when (_countdown == _countdownMax + 1).
Why not just make your display always show 1 less than the number of seconds remaining. That way when you get to zero, (obviously with a Math.Max(0, _countdown) to prevent showing -1) it will seem like the time has run out even though there's one more second to go.
Edit: What I meant to imply but did not state - was that you could then just have one Tick handler and not use Sleep at all which will just wind up blocking the UI anyway which will probably block your UI from updating.
I don't think that events guarantee that event handlers are triggered in the order that they are registered. Try
_countdownTimer.Tick += new EventHandler(delegate(object s, EventArgs a)
{
UpdateCountdown();
if (_countdown == _countdownMax)
{
System.Threading.Thread.Sleep(2000); // Slight delay before taking picture
Camera.TakePicture();
}
});
}

Categories

Resources