Currently developing a simple windows phone 8.1 silverlight app with an implemented countdown time. I have it working where I can input a set amount of minutes and it countdowns fine but what I am wanting to happen is for a user to input an amount of minutes and to countdown in seconds from there, for example it is currently 10, 9, 8, 7, 6, 5 when 10 seconds is input.
What I want to happen is that the user inputs 5 and it counts down like so:
4:59
4:58
4:57
This is my current code:
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += timer_Tick;
basetime = Convert.ToInt32(tbxTime.Text);;
tbxTime.Text = basetime.ToString();
timer.Start();
}
void timer_Tick(object sender, object e)
{
basetime = basetime - 1;
tbxTime.Text = basetime.ToString();
if (basetime == 0)
{
timer.Stop();
}
You can keep most of your existing code if you just make basetime a TimeSpan instead of an int. It's easy to set its value from Minutes or Seconds via the appropriate static method.
var basetime = TimeSpan.FromMinutes(5);
Then you can subtract one second from it like this:
basetime -= TimeSpan.FromSeconds(1);
And display it like this:
tbxTime.Text = basetime.ToString(#"m\:ss");
Finally, comparing it to zero is also trivial:
if (basetime <= TimeSpan.Zero)
See Custom TimeSpan Format Strings for more display options.
I think you can just make use of suitable formatting of TimeSpan class (you will surely find many examples on SO). The easy example can look like this (I assume that you have a TextBox where you enter time and TextBlock which shows counter);
DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };
TimeSpan time;
public MainPage()
{
this.InitializeComponent();
timer.Tick += (sender, e) =>
{
time -= TimeSpan.FromSeconds(1);
if (time <= TimeSpan.Zero) timer.Stop();
myTextBlock.Text = time.ToString(#"mm\:ss");
};
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{ time = TimeSpan.FromMinutes(int.Parse((sender as TextBox).Text)); }
private void startBtn_Click(object sender, RoutedEventArgs e)
{ timer.Start(); }
Note that this is a very simple example. You should also think if DispatcherTimer is a good idea - it works on dispatcher, so in case you have some big job running on main UI thread it may not count the time properly. In this case you may think of using different timer, for example System.Threading.Timer, this runs on separate thread, so you will have to update your UI through Dispatcher.
Related
My android app uses a timer in a certain place.
I get an exception when the time exceeds an hour (3600000), it says the period is too large.
myTime = "3600000";
TempTimer = new System.Threading.Timer ((o) => {
ContentCheck(); // function call/ Void call <----------
}, null, 0, Int64.Parse(myTime) );
I've tried int.parse() already, so tried int64 (Hence it being in code..)
Is there a timer that can do an hour AND longer? Or perhaps and alternative method to get the same results as a timer?
Timer timer = new Timer();
timer.Interval = 3600000;
timer.AutoReset = false;
timer.Start ();
timer.Elapsed+= Timer_Elapsed;
void Timer_Elapsed (object sender, ElapsedEventArgs e)
{
Console.WriteLine("Timer has gone off");
}
Here the interval property of timer instance is of type Double. So that can store really large values. So this should work for you.
I have a windows form application with time input(in minutes) which fires a GUI application after the timer elapses. Initially I take the input from the user and set the time. Say, the user enters 45 mins. After 45 mins, my other GUI application is launched. Currently I'm using this:
Timer MyTimer = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
MyTimer.Interval = 45mins // Input from user
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}
private void MyTimer_Tick(object sender, EventArgs e)
{
//pop my GUI application
}
so now, my question is, how can i extended the timer? Suppose while counting down in the 20th Minute, the user wishes to extend 15mins of the timer, i take the input as 15 from the user and after that, the timer should add this 15 mins to the existing time and fire the GUI app after 35mins. i.e, it should count from 35mins.In total after the time elapses, it would have been 50mins. How can I achieve this?
Actually setting the timer to 1 second is just fine. there will be no performance hit. just keep track of the DateTime when it started, then you can use the tick event to display the elapsed time and check if that duration is greater than what the user wants
private DateTime timerStart;
private TimeSpan duration;
private void Form1_Load(object sender, EventArgs e)
{
Timer MyTimer = new Timer();
MyTimer.Interval = 1000; // tick at one second to update the UI
MyTimer.Tick += new EventHandler(MyTimer_Tick);
duration = whatever...// Input from user
timerStart = DateTime.Now;
MyTimer.Start();
}
private void changeTimer(TimeSpan newValue) {
duration = newValue;
}
private void MyTimer_Tick(object sender, EventArgs e)
{
TimeSpan alreadyElapsed = DateTime.Now.Subtract(timerStart);
// update the UI here using the alreadyElapsed TimeSpan
if(alreadyElapsed > duration)
{
//pop my GUI application
}
}
That's easy to implement if you set your timer to a one second/minute interval and another variable to the number of seconds/minutes.
Decrease the variable value on each timer tick. Add to that variable if you need to expand the interval. If the variable value is 0,launch the other application.
I have created 2 timers and dispatcherTimer.Interval should be updated in the EventHandler for dispatcherTimer2. I have set a default value for the timer and on running the code I can see that it is getting updated but the EventHandler, dispatcherTimer_Tick is called after the default interval. I am not able to solve this problem.
Where am I going wrong and how do I fix this?
System.Timers.Timer dispatcherTimer = new System.Timers.Timer();
dispatcherTimer.Elapsed += dispatcherTimer_Tick;
System.Timers.Timer dispatcherTimer2 = new System.Timers.Timer();
dispatcherTimer2.Elapsed += dispatcherTimer_Tick2;
dispatcherTimer2.Interval = 10000;
dispatcherTimer2.Start();
dispatcherTimer.Interval = 120000;
dispatcherTimer.Start();
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
VideoPlay.Dispatcher.Invoke(new Action(() =>
{
VideoPlay.Source = new Uri("http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test2_Talkinghead_mp4_480x320.mp4");
}));
}
private void dispatcherTimer_Tick2(object sender, EventArgs e)
{
//System.Windows.MessageBox.Show(VideoDay + VideoHr.ToString()+videoMin.ToString());
if(hourparameter==VideoHr && minparameter==videoMin && dayparameter==VideoDay)
{
return;
}
else
{
if (VideoHr == hour)
{
if (day == VideoDay)
{
if (videoMin > min)
{
dispatcherTimer.Enabled = false;
dispatcherTimer = new System.Timers.Timer();
dispatcherTimer.Interval = (videoMin - min) * 60 * 1000;
System.Windows.MessageBox.Show(dispatcherTimer.Interval.ToString());
dispatcherTimer.Enabled = true;
}
EDIT: I have tried with DispatcherTimer again and also checked the times when the EventHandlers are called with some DateTime.Now functions. The problem is still there. if somebody wants I will put up the DispatcherTimer code. I didn't replace the Timers.Timercode in the edit because it would've changed the question. It's basically the same except for the syntax. The code is structured the same way.
EDIT: If I remove the default initialization for the timer interval it just calls the EventHandler continously.But at the same time the 2nd timer eventhandler is also getting called which in turns updates the Interval for the 1st timer. But it never gets used.
I can't understand what I'm doing wrong.
System.Timers.Timer queues ticks in threadpool so you can't be sure that it stops when turn timer's Enabled = false.
You could try to set AutoReset = false at beginning. This makes sure that your timers are run only once but you have start them manually again in the tick code.
dispatcherTimer.AutoReset = false;
dispatcherTimer2.AutoReset = false;
Then you could just replace your timer code with this in dispatcherTimer_Tick2 to fire up timer again.
dispatcherTimer.Interval = (videoMin - min) * 60 * 1000;
dispatcherTimer.Start();
And in the end of dispatcherTimer_Tick also
dispatcherTimer.Start();
I'm not sure what kind behaviour you want but I hope that this helps you.
I have a windows 8 game app that uses a timer for each level and I am trying to the timer 3 seconds.
The code I have to start the timer works but I can't seem to delay the timer so that I display words to count it down.
Here is the code:
private async void timer_Tick(object sender, object e)
{
await
Time.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low,
() =>
{ Time.Text = string.Format("{0}:{1}", (Counter/60), (Counter%60).ToString().PadLeft(2, ' ')); });
Counter--;
await Task.Delay(3000);}}
so I put async there because I thought I should put await Task.Delay(3000); after counter to delay the timer but it doesn't work. I do not want to the WinRT Xaml toolkit countdown timer because I don't want the animation there.
Any suggestions on what I am doing wrong would be great!
So presumably you want to update the display every second, to get this countdown? But if I've understood, for some reason you want to delay everything by 3 seconds.
If you want things to happen 3 seconds later than they're currently happening, the obvious solution is to program the timer so it calls you when you actually want it to:
private DispatcherTimer t = new DispatcherTimer();
private int Counter = 120;
public MainPage()
{
InitializeComponent();
t.Interval = TimeSpan.FromSeconds(4);
t.Tick += timer_Tick;
t.Start();
}
private void timer_Tick(object sender, object o)
{
t.Interval = TimeSpan.FromSeconds(1);
Time.Text = string.Format("{0}:{1}", (Counter / 60), (Counter % 60).ToString().PadLeft(2, ' '));
Counter--;
}
That makes the first tick take 4 seconds to arrive, and then adjusts the interval to 1 second. So you'll get ticks spaced at 1 second intervals, but everything will happen 3 seconds later than it otherwise would. (If set the tick interval to 1 initially, then the first tick would take 1 second to arrive, which is why you need a delay of 4 seconds initially - an initial tick of 3 would only delay things by 2 seconds.)
However, if you need to do some things immediately, and some with a delay, one obvious way to do that would be just to adjust the count in your handler by 3 seconds:
private const int LevelMaxTime = 120;
private DispatcherTimer t = new DispatcherTimer();
private int Counter = LevelMaxTime;
public MainPage()
{
InitializeComponent();
t.Interval = TimeSpan.FromSeconds(1);
t.Tick += timer_Tick;
t.Start();
}
private void timer_Tick(object sender, object o)
{
// Do the undelayed work here, whatever that is...
// Next, we do the delayed work, if there is any yet.
int effectiveCount = Counter + 3;
if (effectiveCount <= LevelMaxTime)
{
Time.Text = string.Format("{0}:{1}", (effectiveCount / 60), (effectiveCount % 60).ToString().PadLeft(2, ' '));
}
Counter--;
}
This just takes the effective current time to be 3 seconds before what Current says it is, thus delaying everything by 3 seconds.
You could do something more like your original code:
private async void timer_Tick(object sender, object o)
{
// Wait for 3 seconds...for some reason
await Task.Delay(TimeSpan.FromSeconds(3));
Time.Text = string.Format("{0}:{1}", (Counter / 60), (Counter % 60).ToString().PadLeft(2, ' '));
Counter--;
}
That's closer in spirit to what you wrote (as far as I can tell), only it works, but it seems unnecessarily convoluted. Why not just program the timer to call you at the right time, rather than getting callbacks at the wrong time and then trying to compensate? It's a timer. It'll call you when you tell it to!
I have a method.
public bool bBIntersectsBT(Rect barTopTipRect, Rect barBottomTipRect, Rect blueBallRect)
{
barTopTipRect.Intersect(blueBallRect);
barBottomTipRect.Intersect(blueBallRect);
if (barTopTipRect.IsEmpty && barBottomTipRect.IsEmpty)
{
return false;
}
else
{
return true;
}
}
I want to delay this method for 2 seconds before this method is executed again. I have read up about Thread.Sleep, However, that is not what I want. I do not want the program to pause and resume it.
Use a DispatcherTimer:
DispatcherTimer timer = new DispatcherTimer();
//TimeSpan is in format: Days, hours, minutes, seconds, milliseconds.
timer.Interval = new TimeSpan(0, 0, 0, 2);
timer.Tick += timerTick;
timer.Start();
private void timerTick(Object sender, EventArgs e)
{
//Your code you want to execute every 2 seconds
//If you want to stop after the two seconds just add timer.Stop() here
}
You can user Dispatch Timer to achieve your goal. Set it to 2 seconds when you want. And after you are done with it. you can stop it.