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.
Related
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();
}
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.
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.
I have a function in winform that is executed every x time (eg. every 60 minutes).
And then it does some stuff, then I want it to wait some seconds (using a timer) and then execute do some stuff part2.
private void goToFtp(int time)
{
double interval = time* 60 * 1000;
System.Timers.Timer checkForTime = new System.Timers.Timer(interval);
checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
checkForTime.Enabled = true;
}
System.Windows.Forms.Timer timerDelayWatcher = new System.Windows.Forms.Timer();
private void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
{
.......Do some stuff part1
timerDelayWatcher.Tick += new EventHandler(timerDelayWatcher_Tick); // Everytime timer ticks, timer_Tick will be called
timerDelayWatcher.Interval = (1000) * (5);
timerDelayWatcher.Enabled = true;
timerDelayWatcher.Start();
}
private void timerDelayWatcher_Tick(object sender, EventArgs e)
{
timerDelayWatcher.Stop();
.......Do some stuff part2
}
The problem is that the timerDelayWatcher_Tick is not fired...any ideias why?
You need use:
Thread.Sleep(5000);
But first you need add
using System.Threading;
or use
System.Threading.Thread.Sleep(5000);
on 5000 are the time in milliseconds
Sample
private void timerDelayWatcher_Tick(object sender, EventArgs e)
{
timerDelayWatcher.Stop();
System.Threading.Thread.Sleep(5000);
.......Do some stuff part2
}
Try calling the start method on the system.timers.timer firstly, and I would recommend sticking to one type of timer, and pattern of use, say use the system.timer.timer and do the work you need on elapsed, then restart with and wait for the next elapsed event.
Either that or I would suggest looking at the task library and async flow in .net 4/4.5 and as #Ferri suggests using a Sleep
Take also care on loosing reference to the class containing the timerDelayWatcher member.
If it happens the timer is disposed so no more events...
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!