Timer gets faster on next calls wp8.1 - c#

i have a countdown timer like this:
private DispatcherTimer _timer;
private int _countdown;
private void initialize_timer()
{
_countdown = 100;
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += (s, e) => Tick();
_timer.Start();
}
private void Tick()
{
_countdown--;
if (_countdown == 0)
{
_timer.Stop();
}
TimeSpan time = TimeSpan.FromSeconds(_countdown);
string str = time.ToString(#"dd\:hh\:mm\:ss");
RemainingTime.Text = str;
}
it works fine until i call initialize_timer() again. the timer gets faster on every call. note that _countdown will be a dynamic value based on a future time so it will change on each call.

When you call initialize_timer(), you create a new Timer but your old Timer is still there and ticking. Just remove the lines
_timer = new DispatcherTimer();
_timer.Tick += (s, e) => Tick();
and you'll be OK.

Related

My elapsed time is only going up to 60

How can I create a elapsed time method that goes beyond 60 seconds AND counts only in seconds. My current implementation constantly repeats every 60 seconds.
CODE:
void timer_Tick(object sender, EventArgs e)
{
time = DateTime.Now.Second.ToString();
//DateTime.Now.ToLongTimeString();
}
public void timeSetup()
{
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
//timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
No need to make things harder than necessary:
class TimerClass
{
public int time;
void timer_Tick(object sender, EventArgs e)
{
time++;
}
public void timeSetup()
{
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += timer_Tick;
timer.Start();
}
}
This calls the Tick handler every second and counts the number of times it is called. This can be imprecise for measuring long periods of time. For the long run, use
time = (DateTime.Now - startTime).TotalSeconds;
where startTime is initialized as the time when you start the timer.

Timer for Windows Phone 8

I wish to execute a function at an interval of 1 or so minutes. How can I achieve this in Windows Phone 8. I am not looking for background agents. The app will be running in the foreground. What are my options?
you can use the DispatcherTimer Class
private DispatcherTimer dispatcherTimer;
// Constructor
public MainPage()
{
InitializeComponent();
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//do whatever you want to do here
}
Refer: (http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v=vs.110).aspx)
Try this
public void Start_timer()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(00, 0, 10);
bool enabled = timer.IsEnabled;
timer.Start();
}
void timer_Tick(object sender, object e)
{
//function to execute
}
You can also use ThreadPoolTimer:
TimeSpan period = TimeSpan.FromSeconds(60);
ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) =>
{
// TODO: Work
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// UI update
});
}, period);
One option may be to use DispatcherTimer.
Simply register a callback on the Tick event.

C# timer I can speed up but not slow down

I have a timer event setup and I would like to change how often the timer event happens by reading a number from a text box. If the box is '10' and you click the update button the event would trigger every 10ms then if you changed to '100' and clicked it would happen every 100ms and so on.
When I run the program however, i can speed up the event frequency (e.g. 100ms to 10ms) but I cannot slow it down (e.g. 10ms to 100ms). Here is the piece of my code that changes the timer when I click:
private void TimerButton_Click(object sender, EventArgs e)
{
getTime = ImgTimeInterval.Text;
bool isNumeric = int.TryParse(ImgTimeInterval.Text, out timerMS); //if number place number in timerMS
label2.Text = isNumeric.ToString();
if (isNumeric)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Enabled = false;
timer.Interval = timerMS;
timer.Elapsed += new ElapsedEventHandler(timerEvent);
timer.AutoReset = true;
timer.Enabled = true;
}
}
public void timerEvent(object source, System.Timers.ElapsedEventArgs e)
{
label1.Text = counter.ToString();
counter = (counter + 1) % 100;
}
If anyone knows what I may be doing wrong it would be greatly appreciated.
The problem with this code is, that you create a new Timer each time you click the button. Try to create the timer outside the method. You think it's only goes faster, but instead multiple timers trigger the timerEvent
private System.Timers.Timer _timer;
private void CreateTimer()
{
_timer = new System.Timers.Timer();
_timer.Enabled = false;
_timer.Interval = 100; // default
_timer.Elapsed += new ElapsedEventHandler(timerEvent);
_timer.AutoReset = true;
_timer.Enabled = true;
}
private void TimerButton_Click(object sender, EventArgs e)
{
bool isNumeric = int.TryParse(ImgTimeInterval.Text, out timerMS); //if number place number in timerMS
label2.Text = isNumeric.ToString();
if (isNumeric)
{
_timer.Interval = timerMS;
}
}
public void timerEvent(object source, System.Timers.ElapsedEventArgs e)
{
label1.Text = counter.ToString();
counter = (counter + 1) % 100;
}
Make sure that the CreateTimer is called in the constructor/formload. Also you can now stop the timer within another button event. With _timer.Enabled = false;
You're always creating a new timer and never stopping the old timer. When you "change" it from 100 to 10 your 100ms timer is still firing every 100 ms, so every 100ms two timers are firing at around the same time.
You need to "remember" the old timer so that you can stop it. Or, better yet, just have only one timer that you change the interval on.
private System.Timers.Timer timer = new System.Timers.Timer();
public Form1()
{
timer.Enabled = false;
timer.AutoReset = true;
timer.Elapsed += timerEvent;
}
private void TimerButton_Click(object sender, EventArgs e)
{
getTime = ImgTimeInterval.Text;
bool isNumeric = int.TryParse(ImgTimeInterval.Text, out timerMS); //if number place number in timerMS
label2.Text = isNumeric.ToString();
if (isNumeric)
{
timer.Interval = timerMS;
timer.Enabled = true;
}
}
Well the basic problem is that you're building a new one every time. Make a private timer:
private System.Timers.Timer _timer = new System.Timers.Timer();
and then fix it up when the button is clicked:
if (isNumeric)
{
_timer.Stop();
_timer.Interval = timerMS;
_timer.Start();
}
and then in the .ctor, do this:
_timer.Elapsed += new ElapsedEventHandler(timerEvent);
Now you have a single timer that you are just modifying as the user changes the value in the text box.

incrementing timer in windows phone

I am having problems with making a Timer App on Windows phone.
I have the text box set to 00:00:00 and im trying to increment it every second but after the first second it wont do any more. I am sure it is an easy fix and would be very appreciative of any help. Thank you
public MainPage()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += OnTimerTick;
timer.Start();
}
void OnTimerTick(object sender, EventArgs args)
{
txtTimer.Text = DateTime.Now.ToString();
}
private void btnStartClick(object sender, EventArgs e)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick +=
delegate(object s, EventArgs args)
{
TimeSpan time = new TimeSpan(0);
time += TimeSpan.FromSeconds(1);
this.timenow.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
};
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}
TimeSpan time = new TimeSpan(0);
time += TimeSpan.FromSeconds(1);
this.timenow.Text = string.Format("{0:D2}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds);
The time variable is created each time the timer ticks. Thus each time you add one second to zero time span. You need to extract it from the delegate. Normally you would make it a class field.

DispatcherTimer apply interval and execute immediately

Basically when we apply some interval ie 5 sec we have to wait for it.
Is it possible to apply interval and execute timer immediately and don't wait 5 sec?
(I mean the interval time).
Any clue?
Thanks!!
public partial class MainWindow : Window
{
DispatcherTimer timer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick);
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void timer_Tick(object sender, EventArgs e)
{
MessageBox.Show("!!!");
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
timer.Interval = new TimeSpan(0, 0, 5);
timer.Start();
}
}
There are definitely more elegant solutions, but a hacky way is to just call the timer_Tick method after you set the interval initially. That'd be better than setting the interval on every tick.
Initially set the interval to zero and then raise it on a subsequent call.
void timer_Tick(object sender, EventArgs e)
{
((Timer)sender).Interval = new TimeSpan(0, 0, 5);
MessageBox.Show("!!!");
}
could try this:
timer.Tick += Timer_Tick;
timer.Interval = 0;
timer.Start();
//...
public void Timer_Tick(object sender, EventArgs e)
{
if (timer.Interval == 0) {
timer.Stop();
timer.Interval = SOME_INTERVAL;
timer.Start();
return;
}
//your timer action code here
}
Another way could be to use two event handlers (to avoid checking an "if" at every tick):
timer.Tick += Timer_TickInit;
timer.Interval = 0;
timer.Start();
//...
public void Timer_TickInit(object sender, EventArgs e)
{
timer.Stop();
timer.Interval = SOME_INTERVAL;
timer.Tick += Timer_Tick();
timer.Start();
}
public void Timer_Tick(object sender, EventArgs e)
{
//your timer action code here
}
However the cleaner way is what was already suggested:
timer.Tick += Timer_Tick;
timer.Interval = SOME_INTERVAL;
SomeAction();
timer.Start();
//...
public void Timer_Tick(object sender, EventArgs e)
{
SomeAction();
}
public void SomeAction(){
//...
}
That's how I solved it:
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
dispatcherTimer.Start();
DispatcherTimer_Tick(dispatcherTimer, new EventArgs());
Works for me without any issues.
Disclaimer: This answer is not for the OP because he wants to use DispatcherTimer
But if you do not have this limitation and you can use another Timer, then there is a cleaner solution
You can use System.Threading.Timer
The most important thing is setting dueTime:0
System.Threading.Timer timer = new Timer(Callback, null, dueTime:0, period:10000);
The documentation of the dueTime is the following
The amount of time to delay before callback is invoked, in milliseconds. Specify Infinite to prevent the timer from starting. Specify zero (0) to start the timer immediately.
and your callback is like this
private void Callback(object? state) =>
{
}
Again this does not use DispatcherTimer but it could solve your problem
Related answer

Categories

Resources