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.
Related
I have simple timer:
private System.Timers.Timer _timer;
public Ctor()
{
_timer = new System.Timers.Timer(timeout);
_timer.Elapsed += Timer_Elapsed;
_timer.Start();
}
How to make that timer rise Elapsed event after timeout time expired?
It is for the first elapsed event, of course.
A Timer does exactly what you want , it raises the event after the specified interval elapses. If you don't want it to be recurring (Runs once only), then set the AutoResetProperty:
public Ctor()
{
_timer = new System.Timers.Timer(timeout);
_timer.AutoReset = false;
_timer.Elapsed += Timer_Elapsed;
_timer.Start();
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
// your code here runs after the timeout elapsed
}
I think you're asking for a timer to be started after an initial delay? If that's the case then consider using a System.Threading.Timers.Timer instead, as follows:
int initialDelay = 5000; // five seconds
int timerPeriod = 1000; // one second
var timer = new Timer(_ => MethodToCallWhenExpired(), null, initialDelay, timerPeriod);
Alternatively you can use Timeout.Infinite and then call the Change() method to alter the Timer behaviour after creation.
Sorry if that's not what you were asking though! :)
I am trying to complete my college assignment, which asks to create a time driven event. A report should be automatically emailed to a responsible person twice everyday. I need to get the times from a database which I can do quite easily. For now, I have created a timer like this:
private static void getDate()
{
DateTime now = DateTime.Now;
DateTime alarm = new DateTime(2015, 10, 27, 19, 23, 0);
TimeSpan diff = alarm - now;
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to the difference in time from now to your alarm.
if (diff.TotalMilliseconds > 0)
{
aTimer.Interval = diff.TotalMilliseconds;
aTimer.Enabled = true;
}
else
{
aTimer.Enabled = false;
aTimer.AutoReset = true;
aTimer.Dispose();
}
}
When the form is initialized, the getDate() function is executed to start the timer. When the timer hits the specific time, I run this code:
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load("report.xsl");
transformer.Transform("Report.xml", "Report.html");
string html = File.ReadAllText("Report.html");
SendMail(html);
}
After the e-mail is sent to a person, I run the getDate() function again to update the time. However, the timer doesn't seem to stop. It defaults to Interval of 100.00 and runs the OnTimedEvent() is executed. I am not sure what changes I should make. So basically the timer needs to run off two different times during the day. When the report is sent both times, it should default to the earliest time the following day (if that makes any sense).
Thank you for any suggestions.
Timer has a property of AutoReset, set this to false to disable it automatically restarting after the Elapsed event.
e.g.:
System.Timers.Timer aTimer = new System.Timers.Timer
{
AutoReset = false
};
When you call getDate() again it is creating a second (new) instance of Timer. Your first Timer is still ticking. To fix this, you need to move the Timer instantiation out of the getDate() method. Move it to the constructor
//Move this to the constructor
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
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.
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 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.