473,761 Members | 5,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1117
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.T imer 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_grierNOSP AM@.msn.comwrot e:
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.T imer 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.hardands oftware.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_grierNOSP AM@.msn.comwrot e:
>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.hardand software.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...@co mcastXXXXXXX.ne twrote:
On 2008-08-14, pamela fluente <pamelaflue...@ libero.itwrote:


On 14 Ago, 17:52, "Dick Grier" <dick_grierNOSP AM@.msn.comwrot e:
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.T imer 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.hardands oftware.netford etails 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.T imer 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_grierNOSP AM@.msn.comwrot e:
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.T imer 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.hardands oftware.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_grierNOSP AM@.msn.comwrot e:
>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.T imer 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.hardand software.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...@co mcastXXXXXXX.ne twrote:
On 2008-08-15, pamela fluente <pamelaflue...@ libero.itwrote:


On 15 Ago, 18:41, "Dick Grier" <dick_grierNOSP AM@.msn.comwrot e:
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.T imer 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.hardands oftware.netford etails 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
13192
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 disposed, do not seem to end their thread after the timers stop working). For instance, when making a simple timer with the following code: public System.Threading.Timer timerTest; public System.Threading.TimerCallback tCallBack;
10
27693
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 timer again before the thread finishes. To my surprise, the timer never come alive again once it has been turned of in the thread. To test the problem I made a simple Windows Form project, put a Beep() in the timer. It supposed to hear beep beep...
22
37749
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() However, this throws and error: System.Threading.ThreadStateException: Thread is running or terminated; it can not restart.
11
7564
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 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...
2
3956
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 done. // Instance variables private Thread workerThread = null; private System.Threading.Timer workerThreadMonitorTimer = null; /// Code inside a button click event ThreadStart workerThreadStart = new ThreadStart(DoWork);
2
3561
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 is running, a timer function keeps executing every 250ms. The timer function does some real time data processing and generates a bitmap which needs to be displayed on a picture control. This is done using this->Invoke(...) in the form.
3
3845
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 windows.forms.controls and I get no error message about cross thread so I am not sure where the problem is. I have a "SerialPoll_timer" that every second requests status from a device on an RS232 port. I use this to determine if the device is on line. ...
9
1426
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 minute). Visual Basic 2005. Thanks.
11
1483
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? I would like the code below to display message to richtextbox while the timer object is not yet destroy so the main thread shouldn't run. When timer object is destroy, the main thread will start and display the last message, "Timer example done." ...
1
9925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.