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! :)
Related
I have an application that calls static methods in a DLL every 60 seconds as part of a system "self-check" application. When I manually run the methods, they all complete in less than 10 seconds. My problem is the timer.elapsed event is firing twice, one right after the other. To add to that, for each time the timer elapses, the event fires one more time. (e.g. first time it's 2 firings, second it's 3, third it's 4, etc.) I have tried setting the timer.AutoReset = false along with setting timer.Enabled = false at the beginning of the elapsed event and then setting it to true at the end of the event. I've tried resetting the interval in the event. Every post I have found indicates that the above actions should have resolved this problem. Can anyone help me find what I'm missing?
static Timer cycle = new Timer();
static int cycCount = 0;
static void Main(string[] args)
{
Console.WriteLine("Firebird Survivor Auto Cycle Started.");
Console.CancelKeyPress += Console_CancelKeyPress;
cycle.Interval = 60000; //set interval for service checks.
cycle.Elapsed += new ElapsedEventHandler(CycleComplete_Elapsed);
cycle.AutoReset = false;
cycle.Enabled = true;
cycle.Elapsed += CycleComplete_Elapsed;
while (1 == 1) //stop main method from completing indefinitely
{
//WAIT FOR TIMER TO ELAPSE
}
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
cycle = null;
}
static void CycleComplete_Elapsed(object sender, ElapsedEventArgs e) //method triggered by timer
{
cycle.Enabled = false;
cycCount++;
WormholeServiceControls.CheckWormHoleStatus();
TimeControls.CheckTimePl(); //call time check
PegasusServiceControls.CheckPegasusStatus(null);
Console.Clear();
Console.WriteLine("--------------------------");
Console.WriteLine(String.Format("| Successful Cycles: {0} |", cycCount));
Console.WriteLine("--------------------------");
cycle.Enabled = true;
}
It seems your problem comes from the event handling you are doing. You are assigning the Elapsed event more than one time:
cycle.Elapsed += new ElapsedEventHandler(CycleComplete_Elapsed);
cycle.Elapsed += CycleComplete_Elapsed;
Why this two lines?. You will be all right with only this:
cycle.Elapsed += new ElapsedEventHandler(CycleComplete_Elapsed);
I'm trying to figure out how to make it so, after lets say, 1 minute so 60000 milliseconds the console will say hi.
All I have so far is
System.Timers.Timer timer = new System.Timers.Timer(60000);
timer.Start();
But I don't know how to make it so when the timer is done, it will do something.
You can use the elapse event, when 60000 ms has pass the event will be thrown. Example of the elapse event:
class Program
{
static void Main(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer(60000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(elapse); // subscribing to the elapse event
timer.Start(); // start Timer
Console.ReadLine(); // hold compiler until key pressed
}
private static void elapse(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Hy");
}
}
or
void Main()
{
var t = new System.Threading.Timer((s)=>Console.WriteLine("Hi"),null,0,60000);
Console.ReadLine();
}
You can use System.Threading.Thread.Sleep if you only want to do the write once (the timer will run every x seconds):
System.Threading.Thread.Sleep(60000);
Console.WriteLine("something");
What you will want to do is create an event that writes to the console when the timer has elapsed the predefined amount of time.
This is done as follows:
Start by creating your timer and set it to 60s:
var timer = new System.Timers.Timer(60000); //60seconds
Next create an event that will be triggered when the time has elapsed:
private static void MyEvent(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Hello World");
}
Next, bind the timer to that event:
timer.Elapsed += MyEvent;
What this does is tell the computer that when the timer actually starts running in the future and then the timer elapses (60s passes after the timer starts), then the Event called 'MyEvent' will be called which writes to the console.
Finally Start the timer:
timer.Start();
And wait for the even to trigger and write to the console.
In my application I'm using two Timer, each Timer use a BackgroundWorker. Here the declaration:
DispatcherTimer timer1 = new DispatcherTimer();
DispatcherTimer timer2 = new DispatcherTimer();
BackgroundWorker worker1 = new BackgroundWorker();
BackgroundWorker worker2= new BackgroundWorker();
I using timer1 for perform an heavy method with a BackgroundWorker and timer2 for execute another BackgroundWorker that check the content of a file.
In this way I assign the event to BackgroundWorkers:
worker1.DoWork += worker_DoWork;
worker1.RunWorkerCompleted += worker_RunWorkerCompleted;
worker1.WorkerSupportsCancellation = true;
worker2.DoWork += worker_DoWork2;
worker2.RunWorkerCompleted += worker_RunWorkerCompleted2;
worker2.WorkerSupportsCancellation = true;
Now timer1 have a range of 15 minutes so the BackgroundWorker execute the heavy method each 15 minutes. And timer2 have a range of 1 second. With the timer1 all working good, but the problems are coming when I've added the timer2.
As I said before this timer allow me to start a method that read a file through the worker2, this file have a property, if this property change I need to perform some special activity. Until here no problem.
What I did is the following:
//This method is called by MainWindow
public ReadFile()
{
//before this I already assigned to timer1 the tick event and start
timer2.Tick -= new EventHandler(Event_Tick);
timer2.Tick += new EventHandler(Event_Tick);
timer2.Interval = new TimeSpan(0, 0, 1);
timer2.Start();
}
This is the Tick event associated to timer2
private void Event_Tick(object sender, EventArgs e)
{
if (!worker1.IsBusy) //I skip the reading, worker1 is busy
{
timer1.Stop(); //stop the first timer
worker2.RunWorkerAsync();
}
else
{
Console.WriteLine("worker1 is busy!");
}
}
I don't need to add here the DoWork, is just a parsing of a file, very useless for the question. When worker2 complete the task I did this:
private void worker_RunWorkerCompleted2(object sender, RunWorkerCompletedEventArgs e)
{
timer1.Start();
ReadFile();
}
How you can see I start the timer1 again, and execute again the ReadFile method. Now if timer1 has reached the interval, so 15 minutes has passed, should execute the timer1.Tick += new EventHandler(Heavy_Tick); that execute the DoWork to worker1. But the timer1 never start.
I can't figure out to this, what am I doing wrong?
Now I get it!
You want to execute worker1 every 15 minutes and worker2 every second but only when worker1 is not busy. Your problem is this here:
if (!worker1.IsBusy) //I skip the reading, worker1 is busy
{
timer1.Stop(); //stop the first timer
worker2.RunWorkerAsync();
}
and this:
public ReadFile()
{
//before this I already assigned to timer1 the tick event and start
timer2.Tick -= new EventHandler(Event_Tick);
timer2.Tick += new EventHandler(Event_Tick);
timer2.Interval = new TimeSpan(0, 0, 1);
timer2.Start();
}
Set both timer intervals and tick event handlers during startup, e.g. Form_Load()or at the beginning of Main(). Start them there too. You should not have to stop any timer at all!
By setting the interval, all you have to do is handle the Tick() event. Remove your .Start() and Stop() calls from your WorkerCompletedand Tick methods and you should do fine.
So a lot could be going on here but you should make sure that:
You timer isn't storing it's old progress and you are checking for a certain length of time before stopping. This will automatically cause the timer to stop when restarting.
The timer.stop() function is not disposing your object to an un-restart-able state.
You aren't accessing the timer variable through some pointer that is maintain a stopped value. (Unlikely but annoying when it happens)
I'd personally consider just pausing the timer and resetting the progress, instead of fully stopping it since this is causing issues.
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 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.