473,405 Members | 2,272 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,405 software developers and data experts.

What thread does Timer operate on

Do Timers operate on the UI thread, or do they have it's own thread?

I'm trying to assess the pros and cons for using a timer or a thread for a
certain application.

-Jerry
Aug 14 '08 #1
8 1098
The answer is: "It depends." There are several timers available.

System.Windows.Forms.Timer (from the Components section of the Toolbox)
executes in the STAThread of the UI.

System.Timers.Timer creates a separate thread, as does Sytem.Threading.Timer
(these are not exactly equivalent to each other).

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
Aug 14 '08 #2
On 14 Ago, 17:52, "Dick Grier" <dick_grierNOSPAM@.msn.comwrote:
The answer is: *"It depends." *There are several timers available.

System.Windows.Forms.Timer (from the Components section of the Toolbox)
executes in the STAThread of the UI.

System.Timers.Timer creates a separate thread, as does Sytem.Threading.Timer
(these are not exactly equivalent to each other).

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
Seewww.hardandsoftware.netfor details and contact information.
Dear Dick,

I am new to this topic and interested.
I think that I have done some experiment by creating
System.Windows.Forms.Timer
within a thread and it worked fine.

Perhaps you are referring to the situation where one drags the
component on a form ?

Could you explain it better? I have the impression that there no need
to have a UI
to have the System.Windows.Forms.Timer work ... Is that right ? Nor it
necessarily
run on the UI thread...(?) Perhaps I am missing something ...
-P
Aug 14 '08 #3
On 2008-08-14, pamela fluente <pa***********@libero.itwrote:
On 14 Ago, 17:52, "Dick Grier" <dick_grierNOSPAM@.msn.comwrote:
>The answer is: *"It depends." *There are several timers available.

System.Windows.Forms.Timer (from the Components section of the Toolbox)
executes in the STAThread of the UI.

System.Timers.Timer creates a separate thread, as does Sytem.Threading.Timer
(these are not exactly equivalent to each other).

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
Seewww.hardandsoftware.netfor details and contact information.

Dear Dick,

I am new to this topic and interested.
I think that I have done some experiment by creating
System.Windows.Forms.Timer
within a thread and it worked fine.

Perhaps you are referring to the situation where one drags the
component on a form ?

Could you explain it better? I have the impression that there no need
to have a UI
to have the System.Windows.Forms.Timer work ... Is that right ? Nor it
necessarily
run on the UI thread...(?) Perhaps I am missing something ...
-P
You are... The System.Windows.Forms.Timer requires a message pump on the
thread it is on to work. It is really a wrapper dealing with WM_TIMER
notification's. If you search, you will find that this is a common mistake
that people make when writing windows services - they will use a
System.Windows.Forms.Timer and then wonder why it never fires :)

--
Tom Shelton
Aug 14 '08 #4
On 14 Ago, 19:23, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:
On 2008-08-14, pamela fluente <pamelaflue...@libero.itwrote:


On 14 Ago, 17:52, "Dick Grier" <dick_grierNOSPAM@.msn.comwrote:
The answer is: *"It depends." *There are several timers available.
System.Windows.Forms.Timer (from the Components section of the Toolbox)
executes in the STAThread of the UI.
System.Timers.Timer creates a separate thread, as does Sytem.Threading..Timer
(these are not exactly equivalent to each other).
Dick
--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
Seewww.hardandsoftware.netfordetails and contact information.
Dear Dick,
*I am new to this topic and interested.
*I think that I have done some experiment by creating
System.Windows.Forms.Timer
*within a thread and it worked fine.
*Perhaps you are referring to the situation where one drags the
component on a form ?
Could you explain it better? I have the impression that there no need
to have a UI
to have the System.Windows.Forms.Timer work ... Is that right ? Nor it
necessarily
run on the UI thread...(?) * Perhaps I am missing something ...
-P

You are... *The System.Windows.Forms.Timer requires a message pump on the
thread it is on to work. *It is really a wrapper dealing with WM_TIMER
notification's. *If you search, you will find that this is a common mistake
that people make when writing windows services - they will use a
System.Windows.Forms.Timer and then wonder why it never fires :)

--
Tom Shelton- Nascondi testo citato

- Mostra testo citato -
hmm This is good to know. So I can create it but it will not work!
Very useful. Thanks a lot for pointing that out.

It's still rather misterious to me why a normal timer will not work
and
not sure why that "message pump" is needed... but I guess I will find
out
with practice ... :-)

-P

Aug 14 '08 #5
While it is possible for the forms Timer to work without a form, a message
pump is required... So, it isn't practical for you to attempt to use this
timer in an application that has no UI (IMO). The additional code needed is
too much to make it worthwhile.

This is one (of the) reason(s) that the System.Timers.Timer object is
available.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
Aug 15 '08 #6
On 15 Ago, 18:41, "Dick Grier" <dick_grierNOSPAM@.msn.comwrote:
While it is possible for the forms Timer to work without a form, a message
pump is required... So, it isn't practical for you to attempt to use this
timer in an application that has no UI (IMO). *The additional code needed is
too much to make it worthwhile.

This is one (of the) reason(s) that the System.Timers.Timer object is
available.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
Seewww.hardandsoftware.netfor details and contact information.
Thank you. I have studied now the timers class and I think I am going
to replace most of my times with this one.

One thing that is *now* misterious to me is that, while I did not
realize the working of the "form" timer, is that
I do have a few timer defined several istances of a class that is not
attached to a GUI. And it does fire and works perfectly fine.

I am really puzzled about this and now I do not understand how is it
possible that those timers are working (?)

By some magic, looks like that .NET is able to "reconnect" those
timers to some form of the application. But I have no idea how this is
possible !?
Any idea?

-P

Aug 15 '08 #7
On 2008-08-15, pamela fluente <pa***********@libero.itwrote:
On 15 Ago, 18:41, "Dick Grier" <dick_grierNOSPAM@.msn.comwrote:
>While it is possible for the forms Timer to work without a form, a message
pump is required... So, it isn't practical for you to attempt to use this
timer in an application that has no UI (IMO). *The additional code needed is
too much to make it worthwhile.

This is one (of the) reason(s) that the System.Timers.Timer object is
available.

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
Seewww.hardandsoftware.netfor details and contact information.

Thank you. I have studied now the timers class and I think I am going
to replace most of my times with this one.

One thing that is *now* misterious to me is that, while I did not
realize the working of the "form" timer, is that
I do have a few timer defined several istances of a class that is not
attached to a GUI. And it does fire and works perfectly fine.

I am really puzzled about this and now I do not understand how is it
possible that those timers are working (?)

By some magic, looks like that .NET is able to "reconnect" those
timers to some form of the application. But I have no idea how this is
possible !?
Any idea?

-P
They will work if:

1) they are in a windows forms application
2) the instances of the class containing them is created on the same thread as
the form that instantiated them...

They will work because there is a message pump on the thread that created
them.

--
Tom Shelton
Aug 15 '08 #8
On 15 Ago, 23:39, Tom Shelton <tom_shel...@comcastXXXXXXX.netwrote:
On 2008-08-15, pamela fluente <pamelaflue...@libero.itwrote:


On 15 Ago, 18:41, "Dick Grier" <dick_grierNOSPAM@.msn.comwrote:
While it is possible for the forms Timer to work without a form, a message
pump is required... So, it isn't practical for you to attempt to use this
timer in an application that has no UI (IMO). *The additional code needed is
too much to make it worthwhile.
This is one (of the) reason(s) that the System.Timers.Timer object is
available.
--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
Seewww.hardandsoftware.netfordetails and contact information.
Thank you. I have studied now the timers class and I think I am going
to replace most of my times with this one.
One thing that is *now* misterious to me is that, while I did not
realize the working of the "form" timer, is that
I do have a few timer defined several istances of a class that is not
attached to a GUI. And it does fire and works perfectly fine.
I am really puzzled about this and now I do not understand how is it
possible that those timers are working (?)
By some magic, looks like that .NET is able to "reconnect" those
timers to some form of the application. But I have no idea how this is
possible !?
Any idea?
-P

They will work if:

1) they are in a windows forms application
2) the instances of the class containing them is created on the same thread as
the form that instantiated them...

They will work because there is a message pump on the thread that created
them.

--
Tom Shelton- Nascondi testo citato

- Mostra testo citato -
Thanks Tom. Very helpful.

-P
Aug 16 '08 #9

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

Similar topics

2
by: Amadej | last post by:
Hello everyone I have a beginner's questions about the System.Threading.Timer class behavior. I have been observing the thread count with Windows Task Manger and noticed that timers, after being...
10
by: Vincent | last post by:
Hi, I was trying to do something in a Thread that starts by a timer. I turn off the timer at the begining of the thread incase it fires again when the thread is still running. Then turn on the...
22
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() ...
11
by: Philip Wagenaar | last post by:
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...
2
by: Dipankar | last post by:
In an Windows application I have created a Timer to monitor some WorkerThread's state. Once the worker thread stops the timer should also dispose after displaying the message that Worker Thread is...
2
by: Amit Dedhia | last post by:
Hi I am developing a scientific application which has moderate level image processing involved. In my application, there is a main application form which invokes another form. When this form...
3
by: Beemer Biker | last post by:
Unaccountably, I cannot re-enable a timer from an background thread. The disable works fine, I just cannot get it to start back up. There is no method "InvokeRequired" like there is for...
9
by: =?Utf-8?B?anVhbg==?= | last post by:
How can I do to start a thread. I tried everything... but it had no work. Any solution? It is a Sub working with a Timer control. I want to do two tasks at the same time (more or less for a...
11
by: winningElevent | last post by:
This code C# is from online source but it was in console application so I modify to work on windows form just for learning on how to use threading.timer, but it doesn't work. Can anyone help me? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.