incrementing timer in windows phone - c#

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.

Related

Timer gets faster on next calls wp8.1

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.

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.

Timer C# use in game development

I have a game in C# and I need to allow tournament mode in which each round will be of 2 minutes. How can I display the time from 0:00 up till 2:00 on the form?
I have this in a constructor:
Timer timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(Timer_Tick);
timer.Start();
And this is the Event Handler
void Timer_Tick(object sender, EventArgs e)
{
this.textBox1.Text = DateTime.Now.ToLongTimeString();
}
but I don't know how I can begin the time from 0:00 instread of the current time.. I tried creating a DateTime instance but when I do myDateTime.ToString(); in the event handler, it just remains 0:00.
I tried searching but I can't find anything related.
Thanks a lot !
Save current time to field when you are starting timer:
_startTime = DateTime.Now;
timer.Start();
And calculate difference later:
void Timer_Tick(object sender, EventArgs e)
{
this.textBox1.Text = (DateTime.Now - _startTime).ToString(#"mm\:ss");
}
You need a member variable that is in scope for both the timer initialization and the Timer_Tick event handler.
class Something
{
DateTime _myDateTime;
Timer _timer;
public Something()
{
_timer = new Timer();
_timer.Interval = 1000;
_timer.Tick += Timer_Tick;
_myDateTime = DateTime.Now;
_timer.Start();
}
void Timer_Tick(object sender, EventArgs e)
{
var diff = DateTime.Now.Subtract(_myDateTime);
this.textBox1.Text = diff.ToString();
}
}
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
void Timer_Tick(object sender, EventArgs e)
{
label1.Text = stopWatch.ElapsedTicks.ToString();
}
You can store a DateTime.Now when you start the timer and then in every timer tick handler calculate how much time has passed between DateTime.Now and the stored start date. If you have a pause, you will need to also keep track of how long the game has been paused.
Considering the inconviniences with the above method, I would suggest you declare a StopWatch somewhere, instantiate and start it where you call timer.Start and then in your timer tick just read the Elapsed property of the StopWatch. You can even Stop and Start (pause) it if you need.

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