473,396 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Does a timer.elapsed start a new thread?

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
Nov 21 '05 #1
11 7505
Philip,

Easy answer, for threading you need to use the threading.timer which is not
the most pleasant class to use.

http://msdn.microsoft.com/library/de...classtopic.asp

(Don't ask me more information than this, I try to avoid this one).

I hope this helps,

Cor

"Philip Wagenaar" <ph*************@online.nospam> schreef in bericht
news:DE**********************************@microsof t.com...
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

Nov 21 '05 #2
"Philip Wagenaar" <ph*************@online.nospam> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
Ok, and how does the normal timer work? (1,2,3 or something else (see my
original post))

"Cor Ligthert [MVP]" wrote:
Philip,

Easy answer, for threading you need to use the threading.timer which is not
the most pleasant class to use.

http://msdn.microsoft.com/library/de...classtopic.asp

(Don't ask me more information than this, I try to avoid this one).

I hope this helps,

Cor

"Philip Wagenaar" <ph*************@online.nospam> schreef in bericht
news:DE**********************************@microsof t.com...
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


Nov 21 '05 #4
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

"Philip Wagenaar" <ph*************@online.nospam> schreef in bericht
news:5D**********************************@microsof t.com...
Ok, and how does the normal timer work? (1,2,3 or something else (see my
original post))

"Cor Ligthert [MVP]" wrote:
Philip,

Easy answer, for threading you need to use the threading.timer which is
not
the most pleasant class to use.

http://msdn.microsoft.com/library/de...classtopic.asp

(Don't ask me more information than this, I try to avoid this one).

I hope this helps,

Cor

"Philip Wagenaar" <ph*************@online.nospam> schreef in bericht
news:DE**********************************@microsof t.com...
> 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


Nov 21 '05 #5
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?
"Cor Ligthert [MVP]" wrote:
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

"Philip Wagenaar" <ph*************@online.nospam> schreef in bericht
news:5D**********************************@microsof t.com...
Ok, and how does the normal timer work? (1,2,3 or something else (see my
original post))

"Cor Ligthert [MVP]" wrote:
Philip,

Easy answer, for threading you need to use the threading.timer which is
not
the most pleasant class to use.

http://msdn.microsoft.com/library/de...classtopic.asp

(Don't ask me more information than this, I try to avoid this one).

I hope this helps,

Cor

"Philip Wagenaar" <ph*************@online.nospam> schreef in bericht
news:DE**********************************@microsof t.com...
> 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


Nov 21 '05 #6
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
Nov 21 '05 #7
"Philip Wagenaar" <ph*************@online.nospam> schrieb:
Ok, and how does the normal timer work? (1,2,3 or something else (see my
original post))


The timer will be blocked.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #8
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:OV**************@TK2MSFTNGP09.phx.gbl...
"Philip Wagenaar" <ph*************@online.nospam> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9
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.

"Herfried K. Wagner [MVP]" wrote:
"Philip Wagenaar" <ph*************@online.nospam> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #10
"TrtnJohn" <Tr******@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #11
Sorry. I had my VB6 brain on when I wrote that. I meant Tick event....

"Herfried K. Wagner [MVP]" wrote:
"TrtnJohn" <Tr******@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Besta | last post by:
Hello all, I am having trouble creating a windows service with a timer. Everything seems to go ok but the elapsed event does not fire.Can anyone shed any light on this, may be something simple as...
10
by: Bob | last post by:
I have a splashscreen running on a thread from Sub Main and it works OK. It displays information in a statusbar about queries. I also have a time on the splashscreen, but the tick event never...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
5
by: Jakub Moskal | last post by:
Hi, I want to write a benchmark that will measure performance of several different algorithms for the same problem. There is a set time bound though, the algorithm cannot run longer than n...
5
by: archana | last post by:
Hi all, I am using timer to do some functionality on user specified time. I am using system.timers.timer class and its timer to do this functionality. What i am doing is i set autoreset to...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
10
by: igor | last post by:
I have recently discovered that the system.Timers.Timer from.Net Framework v1.1 is not reliable when used on Windows 2003 server. When incorporated into a Windows Service, the timer_elapsed event...
4
by: Boki | last post by:
Hi All, I have a timer, if my data queue Q has data, the timer should start work, if there is no data in Q, the timer should stop. However, there is an event can fire timer to start. Should I...
1
by: =?Utf-8?B?QnNtZW5nZW4=?= | last post by:
I create the timer like this: StreamingTimer = new System.Timers.Timer(); StreamingTimer.Elapsed += new System.Timers.ElapsedEventHandler(StreamingProc); StreamingTimer.Interval = 1000;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.