Does a timer.elapsed start a new thread?

G

Guest

Hello,

I am using a timer object in my Windows Forms Application. Does the code in
..elapsed event run in a diffrent thread? If the interval is set to 10
milliseconds and the time to execute the code in the .elapsed event takes 1
secocond to complete, what happens?

1) Timer starts. 10 milliseconds later the code is executed and timer stops.
When code is done, 1 seconds later, the timer continues. 10 milliseconds
later the code is executed and the timer stops. etc etc
2) Timer starts. 10 milliseconds later the code is executed. 10 milliseconds
the code is executed again, etc, etc.
3) Timer starts. 10 milliseconds later the code is executed and timer stops.
1 seconds later the timer starts again. But because the 1 second is longer
then the 10 milliseconds the code is executed right away.
4) Something else?

Philip Wagenaar
 
H

Herfried K. Wagner [MVP]

Philip Wagenaar said:
I am using a timer object in my Windows Forms Application. Does the code
in
.elapsed event run in a diffrent thread?

'System.Windows.Forms.Timer' runs on the same thread.
 
G

Guest

Ok, and how does the normal timer work? (1,2,3 or something else (see my
original post))
 
C

Cor Ligthert [MVP]

Philip,

Your thread text was bringing me probably completely on the wrong track.

Do you disable the timer direct in your tick event. What you describe is
often because of the fact that the timer keeps on firing and the event is
starting again and again.

Just a guess,

Cor
 
G

Guest

I have an object that sends an xml file to a http server. This object is both
created and its sent method is called from within the timer1.elapsed event.

The user can choose if they want the application to run in Serial or
Parallel mode. In serial mode the first thing that happens in the .elapsed
event is timer1.enabled = false.

If in serial mode, it seems logical to assume that the code in the .elapsed
event is only run once at a time. In parallel mode it would be possible that
the code in .elepased event is running more then once in a certian time
frame. But could this cause problems for my application?

This is way I am asking about timers and threads, what is wise?
 
C

Cor Ligthert [MVP]

Philip,

Sorry, this is almost the same part where I gave up a time ago using
threading.timers (I could go around it, however I don't know exactly anymore
how that was).

I was not able to manage the timers in this situation; the wrong ones seemed
to be catched in the wrong place while debugging this seems for *me* almost
impossible.

Therefore just 'Sorry', maybe now you have explained it somebody else knows
an answers that fits us than both.

Cor
 
H

Herfried K. Wagner [MVP]

Philip Wagenaar said:
Ok, and how does the normal timer work? (1,2,3 or something else (see my
original post))

The timer will be blocked.
 
M

m.posseth

if multiple thread may access a procedure ,,, for instance the one that
controls the enabling / dissabling of a timer that fires on the main thread
, then you should implement a locking infrastructure ( threadlock / thread
sync ) this is verry easy in VB.net


regards

Michel Posseth
 
G

Guest

Not completely true. The timer itself does require a new thread. But, the
timer does use Invoke to call the Elapsed delegate which will run on the same
UI thread. The net effect is the same thread that started the timer will
execute the Elapsed event code. This means that if you really need precise
timers you should not use form timers because the Elapsed event cannot run
while the UI thread is busy.
 
H

Herfried K. Wagner [MVP]

TrtnJohn said:
Not completely true. The timer itself does require a new thread. But,
the
timer does use Invoke to call the Elapsed delegate which will run on the
same
UI thread. The net effect is the same thread that started the timer will
execute the Elapsed event code. This means that if you really need
precise
timers you should not use form timers because the Elapsed event cannot run
while the UI thread is busy.

'System.Windows.Forms.Timer' is simply a wrapper around 'SetTimer' and
'KillTimer' functions of "user32.dll". I assume you are referring to one of
the other timers because 'System.Windows.Forms.Timer' doesn't have an
'Elapsed' event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top