How to fire timer.Elapsed event immediately
up vote
45
down vote
favorite
I'm using the System.Timers.Timer
class to create a timer with an Timer.Elapsed
event. The thing is the Timer.Elapsed
event is fired for the first time only after the interval time has passed.
Is there a way to raise the Timer.Elapsed
event right after starting the timer ?
I couldn't find any relevant property in the System.Timers.Timer
class.
c# events timer
add a comment |
up vote
45
down vote
favorite
I'm using the System.Timers.Timer
class to create a timer with an Timer.Elapsed
event. The thing is the Timer.Elapsed
event is fired for the first time only after the interval time has passed.
Is there a way to raise the Timer.Elapsed
event right after starting the timer ?
I couldn't find any relevant property in the System.Timers.Timer
class.
c# events timer
1
can you provide a little bit of code? maybe calling the event handler after setting up the timer could do the trick
– Matten
Aug 3 '11 at 9:09
Yes I will go with calling the event handler. Thx.
– Otiel
Aug 3 '11 at 9:40
add a comment |
up vote
45
down vote
favorite
up vote
45
down vote
favorite
I'm using the System.Timers.Timer
class to create a timer with an Timer.Elapsed
event. The thing is the Timer.Elapsed
event is fired for the first time only after the interval time has passed.
Is there a way to raise the Timer.Elapsed
event right after starting the timer ?
I couldn't find any relevant property in the System.Timers.Timer
class.
c# events timer
I'm using the System.Timers.Timer
class to create a timer with an Timer.Elapsed
event. The thing is the Timer.Elapsed
event is fired for the first time only after the interval time has passed.
Is there a way to raise the Timer.Elapsed
event right after starting the timer ?
I couldn't find any relevant property in the System.Timers.Timer
class.
c# events timer
c# events timer
edited Aug 3 '14 at 23:27
zx81
32.6k85484
32.6k85484
asked Aug 3 '11 at 9:08
Otiel
12.1k1057111
12.1k1057111
1
can you provide a little bit of code? maybe calling the event handler after setting up the timer could do the trick
– Matten
Aug 3 '11 at 9:09
Yes I will go with calling the event handler. Thx.
– Otiel
Aug 3 '11 at 9:40
add a comment |
1
can you provide a little bit of code? maybe calling the event handler after setting up the timer could do the trick
– Matten
Aug 3 '11 at 9:09
Yes I will go with calling the event handler. Thx.
– Otiel
Aug 3 '11 at 9:40
1
1
can you provide a little bit of code? maybe calling the event handler after setting up the timer could do the trick
– Matten
Aug 3 '11 at 9:09
can you provide a little bit of code? maybe calling the event handler after setting up the timer could do the trick
– Matten
Aug 3 '11 at 9:09
Yes I will go with calling the event handler. Thx.
– Otiel
Aug 3 '11 at 9:40
Yes I will go with calling the event handler. Thx.
– Otiel
Aug 3 '11 at 9:40
add a comment |
9 Answers
9
active
oldest
votes
up vote
25
down vote
accepted
Just call the Timer_Tick
method yourself.
If you don't want to deal with the Tick callback method's parameters, then just put the code that was in your Timer_Tick
into another method, and call that from the Timer_Tick and from just after the Timer.Start()
call
As pointed out by @Yahia, you could also use the System.Threading.Timer
timer, which you can set to have an initial delay to 0. Be aware though, that the callback will run on a different thread, as opposed to the callback on the Windows.Forms.Timer
which runs on the UI thread. So if you update any UI controls using the System.Threading.Timer
(without invoking correctly) it'll crash.
Seems the solution for me. But if I want to deal with the Tick callback method's parameters, what should I use?OnTimer(Object source, ElapsedEventArgs e)
My timer instance forsource
? How can I create a newElapsedEventArgs
?
– Otiel
Aug 3 '11 at 9:43
TheTick
Event of theSystem.Windows.Forms.Timer
has asource
andEventArgs
parameter, the source would be the timer, just donew EventArgs()
for the 2nd parameter. If you're using theSystem.Threading.Timer
, then for the 2nd parameter just create a new ElapsedEventArgs, settingSignalTime
to something likeDateTime.Now()
.
– George Duckett
Aug 3 '11 at 9:50
1
OnTimer(timerInstance, new ElapsedEventArgs() { ... });
fill the ElapsedEventArgs-fields as you need them.
– Matten
Aug 3 '11 at 9:50
@Matten Actually I can't -ElapsedEventArgs
has no constructor defined. Am I missing a point?
– Otiel
Aug 3 '11 at 11:47
1
@Leito yes,ElapsedEventArgs
's ctor is marked as internal. So usenull
as second argument and in the handler codeif(e==null) signalTime=DateTime.Now
would yield the same result as initializingElapsedEventArgs
withsignalTime=DateTime.Now
(which isn't possible)
– Matten
Aug 3 '11 at 12:37
|
show 2 more comments
up vote
16
down vote
I just called the **ElapsedEventHandler**
with null parameters.
This saved me a lot of headache.
– Scott Beeson
Sep 6 '16 at 13:05
add a comment |
up vote
10
down vote
not sure about System.Timers.Timer
but try
System.Threading.Timer T = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 30000);
This starts immediately (0 milliseconds for first run, 30000 milliseconds for subsequents runs)...
see
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
http://msdn.microsoft.com/en-us/library/2x96zfy7.aspx
I should have specify that I don't want to use theSystem.Threading.Timers
cause I develop a Windows service. Thx anyway :)
– Otiel
Aug 3 '11 at 9:25
we use System.Threading.Timers in Windows service - are they not allowed or what kind of problems could happen in Windows Service ?
– Yahia
Aug 3 '11 at 9:27
It is written on msdn: System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
– Otiel
Aug 3 '11 at 9:37
1
thanks :-) that's what I read too... so there is nothing prohibiting the use in a Windows Service, only that there is another option with more features.
– Yahia
Aug 3 '11 at 9:40
1
Always favor System.Threading.Timer over System.Timers.Timer, it is a much better timer class.
– Hans Passant
Aug 3 '11 at 10:08
|
show 3 more comments
up vote
9
down vote
I know this answer is late but if you want your System.Timers.Timer
to be fired within 100ms (default interval) then you could simply just initialize the Timer
object without a specified interval, then set the interval within the called function to whatever you like. Here is an example of what I use in my Windows Service:
private static Timer _timer;
protected override void OnStart(string args)
{
_timer = new Timer(); //This will set the default interval
_timer.AutoReset = false;
_timer.Elapsed = OnTimer;
_timer.Start();
}
private void OnTimer(object sender, ElapsedEventArgs args)
{
//Do some work here
_timer.Stop();
_timer.Interval = 50000; //Set your new interval here
_timer.Start();
}
add a comment |
up vote
4
down vote
If you want to be able to raise the event whenever you want (not only just at the moment you start the timer), you can encapsulate a timer in your own MyTimer class. This class exposes the original Timer methods and properties. Furthermore I added an event with explicit add and remove. In this way whenever you add a delegate to the event this is added to both the private MyTimer's event and to the original timer Elapsed event. This means that the timer triggers Elapsed in the usual way, but you can manually trigger the event calling RaiseElapsed (this should sound much simpler looking at the code).
public class MyTimer
{
Timer t = new Timer();
event ElapsedEventHandler timerElapsed;
public event ElapsedEventHandler Elapsed
{
add
{
t.Elapsed += value;
timerElapsed += value;
}
remove
{
t.Elapsed -= value;
timerElapsed -= value;
}
}
public double Interval
{
get
{
return t.Interval;
}
set
{
t.Interval = value;
}
}
public void Start()
{
t.Start();
}
public void Stop()
{
t.Stop();
}
public void RaiseElapsed()
{
if (timerElapsed != null)
timerElapsed(null, null);
}
}
Good idea, just too much for what I want to do :)
– Otiel
Aug 3 '11 at 13:00
add a comment |
up vote
3
down vote
Task.Run(() =>
{
Timer_Elapsed(null, null);
});
After Timer creation/configuration, worked fine for me...
Why was this down voted? This will work and answers the question with a working example.
– rolls
Sep 11 '17 at 7:06
add a comment |
up vote
2
down vote
For the first time, the timer will start after 1 second. After that, its interval will be changed to every 30 seconds or whatever...
//main function
Timer timer = new Timer(1000); //initial start after 1 second
timer.Elapsed += new ElapsedEventHandler(TimerElapsedMethod);
timer.Start();
}
private void TimerElapsedMethod(object sender, ElapsedEventArgs e)
{
Timer timer = (Timer)sender; // Get the timer that fired the event
timer.Interval = 30000; // Change the interval to whatever
.
.
.
}
add a comment |
up vote
0
down vote
I have implemented in VB.NET with AddHandler
Public Class clsMain Inherits ServiceBase
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
gTimer.Interval = 1000
gTimer.Enabled = True
AddHandler gTimer.Elapsed, AddressOf gTimer_Elapsed
End Sub
'When the timer is elapsed this event will be fired
Protected Sub gtimer_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs)
'Stop the timer and do some work
gTimer.Stop()
'Custom code
'Here
'Start to raise the elapsed event again
gTimer.Start()
End Sub
End Class
add a comment |
up vote
0
down vote
Here is the easy answer. (Assuming you're using a Windows form)
After dragging your timer control to your form, set the enabled property to true, and the Interval property to whatever you want the initial value to be. (100 is the default and will start the tick event immediately)
Then within your tick event, simply check the value of the Interval property. If it is not your desired value, set it. It's that simple. The same can be accomplished in your C# code if creating a timer on the fly.
add a comment |
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
25
down vote
accepted
Just call the Timer_Tick
method yourself.
If you don't want to deal with the Tick callback method's parameters, then just put the code that was in your Timer_Tick
into another method, and call that from the Timer_Tick and from just after the Timer.Start()
call
As pointed out by @Yahia, you could also use the System.Threading.Timer
timer, which you can set to have an initial delay to 0. Be aware though, that the callback will run on a different thread, as opposed to the callback on the Windows.Forms.Timer
which runs on the UI thread. So if you update any UI controls using the System.Threading.Timer
(without invoking correctly) it'll crash.
Seems the solution for me. But if I want to deal with the Tick callback method's parameters, what should I use?OnTimer(Object source, ElapsedEventArgs e)
My timer instance forsource
? How can I create a newElapsedEventArgs
?
– Otiel
Aug 3 '11 at 9:43
TheTick
Event of theSystem.Windows.Forms.Timer
has asource
andEventArgs
parameter, the source would be the timer, just donew EventArgs()
for the 2nd parameter. If you're using theSystem.Threading.Timer
, then for the 2nd parameter just create a new ElapsedEventArgs, settingSignalTime
to something likeDateTime.Now()
.
– George Duckett
Aug 3 '11 at 9:50
1
OnTimer(timerInstance, new ElapsedEventArgs() { ... });
fill the ElapsedEventArgs-fields as you need them.
– Matten
Aug 3 '11 at 9:50
@Matten Actually I can't -ElapsedEventArgs
has no constructor defined. Am I missing a point?
– Otiel
Aug 3 '11 at 11:47
1
@Leito yes,ElapsedEventArgs
's ctor is marked as internal. So usenull
as second argument and in the handler codeif(e==null) signalTime=DateTime.Now
would yield the same result as initializingElapsedEventArgs
withsignalTime=DateTime.Now
(which isn't possible)
– Matten
Aug 3 '11 at 12:37
|
show 2 more comments
up vote
25
down vote
accepted
Just call the Timer_Tick
method yourself.
If you don't want to deal with the Tick callback method's parameters, then just put the code that was in your Timer_Tick
into another method, and call that from the Timer_Tick and from just after the Timer.Start()
call
As pointed out by @Yahia, you could also use the System.Threading.Timer
timer, which you can set to have an initial delay to 0. Be aware though, that the callback will run on a different thread, as opposed to the callback on the Windows.Forms.Timer
which runs on the UI thread. So if you update any UI controls using the System.Threading.Timer
(without invoking correctly) it'll crash.
Seems the solution for me. But if I want to deal with the Tick callback method's parameters, what should I use?OnTimer(Object source, ElapsedEventArgs e)
My timer instance forsource
? How can I create a newElapsedEventArgs
?
– Otiel
Aug 3 '11 at 9:43
TheTick
Event of theSystem.Windows.Forms.Timer
has asource
andEventArgs
parameter, the source would be the timer, just donew EventArgs()
for the 2nd parameter. If you're using theSystem.Threading.Timer
, then for the 2nd parameter just create a new ElapsedEventArgs, settingSignalTime
to something likeDateTime.Now()
.
– George Duckett
Aug 3 '11 at 9:50
1
OnTimer(timerInstance, new ElapsedEventArgs() { ... });
fill the ElapsedEventArgs-fields as you need them.
– Matten
Aug 3 '11 at 9:50
@Matten Actually I can't -ElapsedEventArgs
has no constructor defined. Am I missing a point?
– Otiel
Aug 3 '11 at 11:47
1
@Leito yes,ElapsedEventArgs
's ctor is marked as internal. So usenull
as second argument and in the handler codeif(e==null) signalTime=DateTime.Now
would yield the same result as initializingElapsedEventArgs
withsignalTime=DateTime.Now
(which isn't possible)
– Matten
Aug 3 '11 at 12:37
|
show 2 more comments
up vote
25
down vote
accepted
up vote
25
down vote
accepted
Just call the Timer_Tick
method yourself.
If you don't want to deal with the Tick callback method's parameters, then just put the code that was in your Timer_Tick
into another method, and call that from the Timer_Tick and from just after the Timer.Start()
call
As pointed out by @Yahia, you could also use the System.Threading.Timer
timer, which you can set to have an initial delay to 0. Be aware though, that the callback will run on a different thread, as opposed to the callback on the Windows.Forms.Timer
which runs on the UI thread. So if you update any UI controls using the System.Threading.Timer
(without invoking correctly) it'll crash.
Just call the Timer_Tick
method yourself.
If you don't want to deal with the Tick callback method's parameters, then just put the code that was in your Timer_Tick
into another method, and call that from the Timer_Tick and from just after the Timer.Start()
call
As pointed out by @Yahia, you could also use the System.Threading.Timer
timer, which you can set to have an initial delay to 0. Be aware though, that the callback will run on a different thread, as opposed to the callback on the Windows.Forms.Timer
which runs on the UI thread. So if you update any UI controls using the System.Threading.Timer
(without invoking correctly) it'll crash.
edited Aug 27 '14 at 16:13
Martin Thompson
14.5k12744
14.5k12744
answered Aug 3 '11 at 9:10
George Duckett
24.2k578142
24.2k578142
Seems the solution for me. But if I want to deal with the Tick callback method's parameters, what should I use?OnTimer(Object source, ElapsedEventArgs e)
My timer instance forsource
? How can I create a newElapsedEventArgs
?
– Otiel
Aug 3 '11 at 9:43
TheTick
Event of theSystem.Windows.Forms.Timer
has asource
andEventArgs
parameter, the source would be the timer, just donew EventArgs()
for the 2nd parameter. If you're using theSystem.Threading.Timer
, then for the 2nd parameter just create a new ElapsedEventArgs, settingSignalTime
to something likeDateTime.Now()
.
– George Duckett
Aug 3 '11 at 9:50
1
OnTimer(timerInstance, new ElapsedEventArgs() { ... });
fill the ElapsedEventArgs-fields as you need them.
– Matten
Aug 3 '11 at 9:50
@Matten Actually I can't -ElapsedEventArgs
has no constructor defined. Am I missing a point?
– Otiel
Aug 3 '11 at 11:47
1
@Leito yes,ElapsedEventArgs
's ctor is marked as internal. So usenull
as second argument and in the handler codeif(e==null) signalTime=DateTime.Now
would yield the same result as initializingElapsedEventArgs
withsignalTime=DateTime.Now
(which isn't possible)
– Matten
Aug 3 '11 at 12:37
|
show 2 more comments
Seems the solution for me. But if I want to deal with the Tick callback method's parameters, what should I use?OnTimer(Object source, ElapsedEventArgs e)
My timer instance forsource
? How can I create a newElapsedEventArgs
?
– Otiel
Aug 3 '11 at 9:43
TheTick
Event of theSystem.Windows.Forms.Timer
has asource
andEventArgs
parameter, the source would be the timer, just donew EventArgs()
for the 2nd parameter. If you're using theSystem.Threading.Timer
, then for the 2nd parameter just create a new ElapsedEventArgs, settingSignalTime
to something likeDateTime.Now()
.
– George Duckett
Aug 3 '11 at 9:50
1
OnTimer(timerInstance, new ElapsedEventArgs() { ... });
fill the ElapsedEventArgs-fields as you need them.
– Matten
Aug 3 '11 at 9:50
@Matten Actually I can't -ElapsedEventArgs
has no constructor defined. Am I missing a point?
– Otiel
Aug 3 '11 at 11:47
1
@Leito yes,ElapsedEventArgs
's ctor is marked as internal. So usenull
as second argument and in the handler codeif(e==null) signalTime=DateTime.Now
would yield the same result as initializingElapsedEventArgs
withsignalTime=DateTime.Now
(which isn't possible)
– Matten
Aug 3 '11 at 12:37
Seems the solution for me. But if I want to deal with the Tick callback method's parameters, what should I use?
OnTimer(Object source, ElapsedEventArgs e)
My timer instance for source
? How can I create a new ElapsedEventArgs
?– Otiel
Aug 3 '11 at 9:43
Seems the solution for me. But if I want to deal with the Tick callback method's parameters, what should I use?
OnTimer(Object source, ElapsedEventArgs e)
My timer instance for source
? How can I create a new ElapsedEventArgs
?– Otiel
Aug 3 '11 at 9:43
The
Tick
Event of the System.Windows.Forms.Timer
has a source
and EventArgs
parameter, the source would be the timer, just do new EventArgs()
for the 2nd parameter. If you're using the System.Threading.Timer
, then for the 2nd parameter just create a new ElapsedEventArgs, setting SignalTime
to something like DateTime.Now()
.– George Duckett
Aug 3 '11 at 9:50
The
Tick
Event of the System.Windows.Forms.Timer
has a source
and EventArgs
parameter, the source would be the timer, just do new EventArgs()
for the 2nd parameter. If you're using the System.Threading.Timer
, then for the 2nd parameter just create a new ElapsedEventArgs, setting SignalTime
to something like DateTime.Now()
.– George Duckett
Aug 3 '11 at 9:50
1
1
OnTimer(timerInstance, new ElapsedEventArgs() { ... });
fill the ElapsedEventArgs-fields as you need them.– Matten
Aug 3 '11 at 9:50
OnTimer(timerInstance, new ElapsedEventArgs() { ... });
fill the ElapsedEventArgs-fields as you need them.– Matten
Aug 3 '11 at 9:50
@Matten Actually I can't -
ElapsedEventArgs
has no constructor defined. Am I missing a point?– Otiel
Aug 3 '11 at 11:47
@Matten Actually I can't -
ElapsedEventArgs
has no constructor defined. Am I missing a point?– Otiel
Aug 3 '11 at 11:47
1
1
@Leito yes,
ElapsedEventArgs
's ctor is marked as internal. So use null
as second argument and in the handler code if(e==null) signalTime=DateTime.Now
would yield the same result as initializing ElapsedEventArgs
with signalTime=DateTime.Now
(which isn't possible)– Matten
Aug 3 '11 at 12:37
@Leito yes,
ElapsedEventArgs
's ctor is marked as internal. So use null
as second argument and in the handler code if(e==null) signalTime=DateTime.Now
would yield the same result as initializing ElapsedEventArgs
with signalTime=DateTime.Now
(which isn't possible)– Matten
Aug 3 '11 at 12:37
|
show 2 more comments
up vote
16
down vote
I just called the **ElapsedEventHandler**
with null parameters.
This saved me a lot of headache.
– Scott Beeson
Sep 6 '16 at 13:05
add a comment |
up vote
16
down vote
I just called the **ElapsedEventHandler**
with null parameters.
This saved me a lot of headache.
– Scott Beeson
Sep 6 '16 at 13:05
add a comment |
up vote
16
down vote
up vote
16
down vote
I just called the **ElapsedEventHandler**
with null parameters.
I just called the **ElapsedEventHandler**
with null parameters.
edited Nov 2 '12 at 10:42
vinothp
6,4311446100
6,4311446100
answered Nov 2 '12 at 10:21
user1791983
16112
16112
This saved me a lot of headache.
– Scott Beeson
Sep 6 '16 at 13:05
add a comment |
This saved me a lot of headache.
– Scott Beeson
Sep 6 '16 at 13:05
This saved me a lot of headache.
– Scott Beeson
Sep 6 '16 at 13:05
This saved me a lot of headache.
– Scott Beeson
Sep 6 '16 at 13:05
add a comment |
up vote
10
down vote
not sure about System.Timers.Timer
but try
System.Threading.Timer T = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 30000);
This starts immediately (0 milliseconds for first run, 30000 milliseconds for subsequents runs)...
see
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
http://msdn.microsoft.com/en-us/library/2x96zfy7.aspx
I should have specify that I don't want to use theSystem.Threading.Timers
cause I develop a Windows service. Thx anyway :)
– Otiel
Aug 3 '11 at 9:25
we use System.Threading.Timers in Windows service - are they not allowed or what kind of problems could happen in Windows Service ?
– Yahia
Aug 3 '11 at 9:27
It is written on msdn: System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
– Otiel
Aug 3 '11 at 9:37
1
thanks :-) that's what I read too... so there is nothing prohibiting the use in a Windows Service, only that there is another option with more features.
– Yahia
Aug 3 '11 at 9:40
1
Always favor System.Threading.Timer over System.Timers.Timer, it is a much better timer class.
– Hans Passant
Aug 3 '11 at 10:08
|
show 3 more comments
up vote
10
down vote
not sure about System.Timers.Timer
but try
System.Threading.Timer T = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 30000);
This starts immediately (0 milliseconds for first run, 30000 milliseconds for subsequents runs)...
see
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
http://msdn.microsoft.com/en-us/library/2x96zfy7.aspx
I should have specify that I don't want to use theSystem.Threading.Timers
cause I develop a Windows service. Thx anyway :)
– Otiel
Aug 3 '11 at 9:25
we use System.Threading.Timers in Windows service - are they not allowed or what kind of problems could happen in Windows Service ?
– Yahia
Aug 3 '11 at 9:27
It is written on msdn: System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
– Otiel
Aug 3 '11 at 9:37
1
thanks :-) that's what I read too... so there is nothing prohibiting the use in a Windows Service, only that there is another option with more features.
– Yahia
Aug 3 '11 at 9:40
1
Always favor System.Threading.Timer over System.Timers.Timer, it is a much better timer class.
– Hans Passant
Aug 3 '11 at 10:08
|
show 3 more comments
up vote
10
down vote
up vote
10
down vote
not sure about System.Timers.Timer
but try
System.Threading.Timer T = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 30000);
This starts immediately (0 milliseconds for first run, 30000 milliseconds for subsequents runs)...
see
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
http://msdn.microsoft.com/en-us/library/2x96zfy7.aspx
not sure about System.Timers.Timer
but try
System.Threading.Timer T = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 30000);
This starts immediately (0 milliseconds for first run, 30000 milliseconds for subsequents runs)...
see
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
http://msdn.microsoft.com/en-us/library/2x96zfy7.aspx
answered Aug 3 '11 at 9:15
Yahia
62.4k688120
62.4k688120
I should have specify that I don't want to use theSystem.Threading.Timers
cause I develop a Windows service. Thx anyway :)
– Otiel
Aug 3 '11 at 9:25
we use System.Threading.Timers in Windows service - are they not allowed or what kind of problems could happen in Windows Service ?
– Yahia
Aug 3 '11 at 9:27
It is written on msdn: System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
– Otiel
Aug 3 '11 at 9:37
1
thanks :-) that's what I read too... so there is nothing prohibiting the use in a Windows Service, only that there is another option with more features.
– Yahia
Aug 3 '11 at 9:40
1
Always favor System.Threading.Timer over System.Timers.Timer, it is a much better timer class.
– Hans Passant
Aug 3 '11 at 10:08
|
show 3 more comments
I should have specify that I don't want to use theSystem.Threading.Timers
cause I develop a Windows service. Thx anyway :)
– Otiel
Aug 3 '11 at 9:25
we use System.Threading.Timers in Windows service - are they not allowed or what kind of problems could happen in Windows Service ?
– Yahia
Aug 3 '11 at 9:27
It is written on msdn: System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
– Otiel
Aug 3 '11 at 9:37
1
thanks :-) that's what I read too... so there is nothing prohibiting the use in a Windows Service, only that there is another option with more features.
– Yahia
Aug 3 '11 at 9:40
1
Always favor System.Threading.Timer over System.Timers.Timer, it is a much better timer class.
– Hans Passant
Aug 3 '11 at 10:08
I should have specify that I don't want to use the
System.Threading.Timers
cause I develop a Windows service. Thx anyway :)– Otiel
Aug 3 '11 at 9:25
I should have specify that I don't want to use the
System.Threading.Timers
cause I develop a Windows service. Thx anyway :)– Otiel
Aug 3 '11 at 9:25
we use System.Threading.Timers in Windows service - are they not allowed or what kind of problems could happen in Windows Service ?
– Yahia
Aug 3 '11 at 9:27
we use System.Threading.Timers in Windows service - are they not allowed or what kind of problems could happen in Windows Service ?
– Yahia
Aug 3 '11 at 9:27
It is written on msdn: System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
– Otiel
Aug 3 '11 at 9:37
It is written on msdn: System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.
– Otiel
Aug 3 '11 at 9:37
1
1
thanks :-) that's what I read too... so there is nothing prohibiting the use in a Windows Service, only that there is another option with more features.
– Yahia
Aug 3 '11 at 9:40
thanks :-) that's what I read too... so there is nothing prohibiting the use in a Windows Service, only that there is another option with more features.
– Yahia
Aug 3 '11 at 9:40
1
1
Always favor System.Threading.Timer over System.Timers.Timer, it is a much better timer class.
– Hans Passant
Aug 3 '11 at 10:08
Always favor System.Threading.Timer over System.Timers.Timer, it is a much better timer class.
– Hans Passant
Aug 3 '11 at 10:08
|
show 3 more comments
up vote
9
down vote
I know this answer is late but if you want your System.Timers.Timer
to be fired within 100ms (default interval) then you could simply just initialize the Timer
object without a specified interval, then set the interval within the called function to whatever you like. Here is an example of what I use in my Windows Service:
private static Timer _timer;
protected override void OnStart(string args)
{
_timer = new Timer(); //This will set the default interval
_timer.AutoReset = false;
_timer.Elapsed = OnTimer;
_timer.Start();
}
private void OnTimer(object sender, ElapsedEventArgs args)
{
//Do some work here
_timer.Stop();
_timer.Interval = 50000; //Set your new interval here
_timer.Start();
}
add a comment |
up vote
9
down vote
I know this answer is late but if you want your System.Timers.Timer
to be fired within 100ms (default interval) then you could simply just initialize the Timer
object without a specified interval, then set the interval within the called function to whatever you like. Here is an example of what I use in my Windows Service:
private static Timer _timer;
protected override void OnStart(string args)
{
_timer = new Timer(); //This will set the default interval
_timer.AutoReset = false;
_timer.Elapsed = OnTimer;
_timer.Start();
}
private void OnTimer(object sender, ElapsedEventArgs args)
{
//Do some work here
_timer.Stop();
_timer.Interval = 50000; //Set your new interval here
_timer.Start();
}
add a comment |
up vote
9
down vote
up vote
9
down vote
I know this answer is late but if you want your System.Timers.Timer
to be fired within 100ms (default interval) then you could simply just initialize the Timer
object without a specified interval, then set the interval within the called function to whatever you like. Here is an example of what I use in my Windows Service:
private static Timer _timer;
protected override void OnStart(string args)
{
_timer = new Timer(); //This will set the default interval
_timer.AutoReset = false;
_timer.Elapsed = OnTimer;
_timer.Start();
}
private void OnTimer(object sender, ElapsedEventArgs args)
{
//Do some work here
_timer.Stop();
_timer.Interval = 50000; //Set your new interval here
_timer.Start();
}
I know this answer is late but if you want your System.Timers.Timer
to be fired within 100ms (default interval) then you could simply just initialize the Timer
object without a specified interval, then set the interval within the called function to whatever you like. Here is an example of what I use in my Windows Service:
private static Timer _timer;
protected override void OnStart(string args)
{
_timer = new Timer(); //This will set the default interval
_timer.AutoReset = false;
_timer.Elapsed = OnTimer;
_timer.Start();
}
private void OnTimer(object sender, ElapsedEventArgs args)
{
//Do some work here
_timer.Stop();
_timer.Interval = 50000; //Set your new interval here
_timer.Start();
}
answered Jun 2 '15 at 14:24
John Odom
81821632
81821632
add a comment |
add a comment |
up vote
4
down vote
If you want to be able to raise the event whenever you want (not only just at the moment you start the timer), you can encapsulate a timer in your own MyTimer class. This class exposes the original Timer methods and properties. Furthermore I added an event with explicit add and remove. In this way whenever you add a delegate to the event this is added to both the private MyTimer's event and to the original timer Elapsed event. This means that the timer triggers Elapsed in the usual way, but you can manually trigger the event calling RaiseElapsed (this should sound much simpler looking at the code).
public class MyTimer
{
Timer t = new Timer();
event ElapsedEventHandler timerElapsed;
public event ElapsedEventHandler Elapsed
{
add
{
t.Elapsed += value;
timerElapsed += value;
}
remove
{
t.Elapsed -= value;
timerElapsed -= value;
}
}
public double Interval
{
get
{
return t.Interval;
}
set
{
t.Interval = value;
}
}
public void Start()
{
t.Start();
}
public void Stop()
{
t.Stop();
}
public void RaiseElapsed()
{
if (timerElapsed != null)
timerElapsed(null, null);
}
}
Good idea, just too much for what I want to do :)
– Otiel
Aug 3 '11 at 13:00
add a comment |
up vote
4
down vote
If you want to be able to raise the event whenever you want (not only just at the moment you start the timer), you can encapsulate a timer in your own MyTimer class. This class exposes the original Timer methods and properties. Furthermore I added an event with explicit add and remove. In this way whenever you add a delegate to the event this is added to both the private MyTimer's event and to the original timer Elapsed event. This means that the timer triggers Elapsed in the usual way, but you can manually trigger the event calling RaiseElapsed (this should sound much simpler looking at the code).
public class MyTimer
{
Timer t = new Timer();
event ElapsedEventHandler timerElapsed;
public event ElapsedEventHandler Elapsed
{
add
{
t.Elapsed += value;
timerElapsed += value;
}
remove
{
t.Elapsed -= value;
timerElapsed -= value;
}
}
public double Interval
{
get
{
return t.Interval;
}
set
{
t.Interval = value;
}
}
public void Start()
{
t.Start();
}
public void Stop()
{
t.Stop();
}
public void RaiseElapsed()
{
if (timerElapsed != null)
timerElapsed(null, null);
}
}
Good idea, just too much for what I want to do :)
– Otiel
Aug 3 '11 at 13:00
add a comment |
up vote
4
down vote
up vote
4
down vote
If you want to be able to raise the event whenever you want (not only just at the moment you start the timer), you can encapsulate a timer in your own MyTimer class. This class exposes the original Timer methods and properties. Furthermore I added an event with explicit add and remove. In this way whenever you add a delegate to the event this is added to both the private MyTimer's event and to the original timer Elapsed event. This means that the timer triggers Elapsed in the usual way, but you can manually trigger the event calling RaiseElapsed (this should sound much simpler looking at the code).
public class MyTimer
{
Timer t = new Timer();
event ElapsedEventHandler timerElapsed;
public event ElapsedEventHandler Elapsed
{
add
{
t.Elapsed += value;
timerElapsed += value;
}
remove
{
t.Elapsed -= value;
timerElapsed -= value;
}
}
public double Interval
{
get
{
return t.Interval;
}
set
{
t.Interval = value;
}
}
public void Start()
{
t.Start();
}
public void Stop()
{
t.Stop();
}
public void RaiseElapsed()
{
if (timerElapsed != null)
timerElapsed(null, null);
}
}
If you want to be able to raise the event whenever you want (not only just at the moment you start the timer), you can encapsulate a timer in your own MyTimer class. This class exposes the original Timer methods and properties. Furthermore I added an event with explicit add and remove. In this way whenever you add a delegate to the event this is added to both the private MyTimer's event and to the original timer Elapsed event. This means that the timer triggers Elapsed in the usual way, but you can manually trigger the event calling RaiseElapsed (this should sound much simpler looking at the code).
public class MyTimer
{
Timer t = new Timer();
event ElapsedEventHandler timerElapsed;
public event ElapsedEventHandler Elapsed
{
add
{
t.Elapsed += value;
timerElapsed += value;
}
remove
{
t.Elapsed -= value;
timerElapsed -= value;
}
}
public double Interval
{
get
{
return t.Interval;
}
set
{
t.Interval = value;
}
}
public void Start()
{
t.Start();
}
public void Stop()
{
t.Stop();
}
public void RaiseElapsed()
{
if (timerElapsed != null)
timerElapsed(null, null);
}
}
answered Aug 3 '11 at 9:42
Francesco Baruchelli
5,96722733
5,96722733
Good idea, just too much for what I want to do :)
– Otiel
Aug 3 '11 at 13:00
add a comment |
Good idea, just too much for what I want to do :)
– Otiel
Aug 3 '11 at 13:00
Good idea, just too much for what I want to do :)
– Otiel
Aug 3 '11 at 13:00
Good idea, just too much for what I want to do :)
– Otiel
Aug 3 '11 at 13:00
add a comment |
up vote
3
down vote
Task.Run(() =>
{
Timer_Elapsed(null, null);
});
After Timer creation/configuration, worked fine for me...
Why was this down voted? This will work and answers the question with a working example.
– rolls
Sep 11 '17 at 7:06
add a comment |
up vote
3
down vote
Task.Run(() =>
{
Timer_Elapsed(null, null);
});
After Timer creation/configuration, worked fine for me...
Why was this down voted? This will work and answers the question with a working example.
– rolls
Sep 11 '17 at 7:06
add a comment |
up vote
3
down vote
up vote
3
down vote
Task.Run(() =>
{
Timer_Elapsed(null, null);
});
After Timer creation/configuration, worked fine for me...
Task.Run(() =>
{
Timer_Elapsed(null, null);
});
After Timer creation/configuration, worked fine for me...
answered Jul 24 '17 at 20:43
ADNnetPonzo
967
967
Why was this down voted? This will work and answers the question with a working example.
– rolls
Sep 11 '17 at 7:06
add a comment |
Why was this down voted? This will work and answers the question with a working example.
– rolls
Sep 11 '17 at 7:06
Why was this down voted? This will work and answers the question with a working example.
– rolls
Sep 11 '17 at 7:06
Why was this down voted? This will work and answers the question with a working example.
– rolls
Sep 11 '17 at 7:06
add a comment |
up vote
2
down vote
For the first time, the timer will start after 1 second. After that, its interval will be changed to every 30 seconds or whatever...
//main function
Timer timer = new Timer(1000); //initial start after 1 second
timer.Elapsed += new ElapsedEventHandler(TimerElapsedMethod);
timer.Start();
}
private void TimerElapsedMethod(object sender, ElapsedEventArgs e)
{
Timer timer = (Timer)sender; // Get the timer that fired the event
timer.Interval = 30000; // Change the interval to whatever
.
.
.
}
add a comment |
up vote
2
down vote
For the first time, the timer will start after 1 second. After that, its interval will be changed to every 30 seconds or whatever...
//main function
Timer timer = new Timer(1000); //initial start after 1 second
timer.Elapsed += new ElapsedEventHandler(TimerElapsedMethod);
timer.Start();
}
private void TimerElapsedMethod(object sender, ElapsedEventArgs e)
{
Timer timer = (Timer)sender; // Get the timer that fired the event
timer.Interval = 30000; // Change the interval to whatever
.
.
.
}
add a comment |
up vote
2
down vote
up vote
2
down vote
For the first time, the timer will start after 1 second. After that, its interval will be changed to every 30 seconds or whatever...
//main function
Timer timer = new Timer(1000); //initial start after 1 second
timer.Elapsed += new ElapsedEventHandler(TimerElapsedMethod);
timer.Start();
}
private void TimerElapsedMethod(object sender, ElapsedEventArgs e)
{
Timer timer = (Timer)sender; // Get the timer that fired the event
timer.Interval = 30000; // Change the interval to whatever
.
.
.
}
For the first time, the timer will start after 1 second. After that, its interval will be changed to every 30 seconds or whatever...
//main function
Timer timer = new Timer(1000); //initial start after 1 second
timer.Elapsed += new ElapsedEventHandler(TimerElapsedMethod);
timer.Start();
}
private void TimerElapsedMethod(object sender, ElapsedEventArgs e)
{
Timer timer = (Timer)sender; // Get the timer that fired the event
timer.Interval = 30000; // Change the interval to whatever
.
.
.
}
answered Jan 23 at 0:01
Hamad
101126
101126
add a comment |
add a comment |
up vote
0
down vote
I have implemented in VB.NET with AddHandler
Public Class clsMain Inherits ServiceBase
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
gTimer.Interval = 1000
gTimer.Enabled = True
AddHandler gTimer.Elapsed, AddressOf gTimer_Elapsed
End Sub
'When the timer is elapsed this event will be fired
Protected Sub gtimer_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs)
'Stop the timer and do some work
gTimer.Stop()
'Custom code
'Here
'Start to raise the elapsed event again
gTimer.Start()
End Sub
End Class
add a comment |
up vote
0
down vote
I have implemented in VB.NET with AddHandler
Public Class clsMain Inherits ServiceBase
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
gTimer.Interval = 1000
gTimer.Enabled = True
AddHandler gTimer.Elapsed, AddressOf gTimer_Elapsed
End Sub
'When the timer is elapsed this event will be fired
Protected Sub gtimer_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs)
'Stop the timer and do some work
gTimer.Stop()
'Custom code
'Here
'Start to raise the elapsed event again
gTimer.Start()
End Sub
End Class
add a comment |
up vote
0
down vote
up vote
0
down vote
I have implemented in VB.NET with AddHandler
Public Class clsMain Inherits ServiceBase
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
gTimer.Interval = 1000
gTimer.Enabled = True
AddHandler gTimer.Elapsed, AddressOf gTimer_Elapsed
End Sub
'When the timer is elapsed this event will be fired
Protected Sub gtimer_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs)
'Stop the timer and do some work
gTimer.Stop()
'Custom code
'Here
'Start to raise the elapsed event again
gTimer.Start()
End Sub
End Class
I have implemented in VB.NET with AddHandler
Public Class clsMain Inherits ServiceBase
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
gTimer.Interval = 1000
gTimer.Enabled = True
AddHandler gTimer.Elapsed, AddressOf gTimer_Elapsed
End Sub
'When the timer is elapsed this event will be fired
Protected Sub gtimer_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs)
'Stop the timer and do some work
gTimer.Stop()
'Custom code
'Here
'Start to raise the elapsed event again
gTimer.Start()
End Sub
End Class
answered Mar 19 '14 at 1:15
developer9
744
744
add a comment |
add a comment |
up vote
0
down vote
Here is the easy answer. (Assuming you're using a Windows form)
After dragging your timer control to your form, set the enabled property to true, and the Interval property to whatever you want the initial value to be. (100 is the default and will start the tick event immediately)
Then within your tick event, simply check the value of the Interval property. If it is not your desired value, set it. It's that simple. The same can be accomplished in your C# code if creating a timer on the fly.
add a comment |
up vote
0
down vote
Here is the easy answer. (Assuming you're using a Windows form)
After dragging your timer control to your form, set the enabled property to true, and the Interval property to whatever you want the initial value to be. (100 is the default and will start the tick event immediately)
Then within your tick event, simply check the value of the Interval property. If it is not your desired value, set it. It's that simple. The same can be accomplished in your C# code if creating a timer on the fly.
add a comment |
up vote
0
down vote
up vote
0
down vote
Here is the easy answer. (Assuming you're using a Windows form)
After dragging your timer control to your form, set the enabled property to true, and the Interval property to whatever you want the initial value to be. (100 is the default and will start the tick event immediately)
Then within your tick event, simply check the value of the Interval property. If it is not your desired value, set it. It's that simple. The same can be accomplished in your C# code if creating a timer on the fly.
Here is the easy answer. (Assuming you're using a Windows form)
After dragging your timer control to your form, set the enabled property to true, and the Interval property to whatever you want the initial value to be. (100 is the default and will start the tick event immediately)
Then within your tick event, simply check the value of the Interval property. If it is not your desired value, set it. It's that simple. The same can be accomplished in your C# code if creating a timer on the fly.
answered Nov 19 at 14:21
puffgroovy
11014
11014
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6924072%2fhow-to-fire-timer-elapsed-event-immediately%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
can you provide a little bit of code? maybe calling the event handler after setting up the timer could do the trick
– Matten
Aug 3 '11 at 9:09
Yes I will go with calling the event handler. Thx.
– Otiel
Aug 3 '11 at 9:40