473,513 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timer Control query

Hi All

I am using VB.net 2008 and use timer controls within my applications

Question

Does the code in a Timer control.tick event run on a different thread to the
main Application thread (UI Thread)?

In some of the timers I update some UI controls e.g statusbar.labels and I
am wondering if I should be doing this if the code is running in a different
thread
Regards
Steve
Oct 31 '08 #1
3 3852
Good morning Steve,

Before we look at the two questions in the post, I'd like to first
introduce three different timer classes in the .NET Class Library:

System.Windows.Forms.Timer
System.Timers.Timer
System.Threading.Timer

The first two classes appear in the Visual Studio.NET toolbox window,
allowing us to drag and drop them directly onto a Windows Forms designer or
a component class designer. System.Threading.Timer does not appear in the
toolbox, but it exposes several more advanced features. Alex Calvo [MSFT]
wrote a good article, going through the differences between the three in
detail.

Comparing the Timer Classes in the .NET Framework Class Library
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
>Does the code in a Timer control.tick event run on a different thread
to the main Application thread (UI Thread)?
If the timer is the control dragged from the toolbox into the winform
designer, control.tick event handler would run on the same thread as the UI
thread. It can be proved with this piece of code:

// in the winform, (e.g. Form_Load), we dump the UI thread ID:
Debug.Print("UI Thread: " +
Thread.CurrentThread.ManagedThreadId.ToString());

// in the timer's tick event handler, we dump the ID of the thread that
runs the handler:
private void timer1_Tick(object sender, EventArgs e)
{
Debug.Print("Tick: " +
Thread.CurrentThread.ManagedThreadId.ToString());
}
>In some of the timers I update some UI controls e.g statusbar.labels
and I am wondering if I should be doing this if the code is running in
a different thread
If the timer belongs to System.Windows.Forms.Timer, or System.Timers.Timer
whose SynchronizingObject is set to the winform object (for example:

Dim tmrTimersTimer As New System.Timers.Timer();
tmrTimersTimer.SynchronizingObject = Me 'Synchronize with the current form

the timer's tick event will run in the UI thread, and we can directly
operate on the UI control (e.g. statusbar.labels).

However, if the timer belongs to System.Threading.Timer, or
System.Timers.Timer whose SynchronizingObject is NOT set to the winform
object, the ticket event will run in a different thread, and we need to
call either Control.Invoke or Control.BeginInvoke to operate on the UI
controls. For example:

// Created on UI thread
private Label lblStatus;

// Doesn't run on UI thread
private void RunsOnWorkerThread() {
DoSomethingSlow();
// Do UI update on UI thread
object[] pList = { this, System.EventArgs.Empty };
lblStatus.BeginInvoke(
new System.EventHandler(UpdateUI), pList);
}

// Code to be run back on the UI thread
// (using System.EventHandler signature
// so we don't need to define a new
// delegate type here)
private void UpdateUI(object o, System.EventArgs e) {
// Now OK - this method will be called via
// Control.Invoke, so we are allowed to do
// things to the UI.
lblStatus.Text = "Finished!";
}

For simplicity, we usually write a wrapper function, e.g

public void ShowProgress(string msg, int percentDone) {
if (InvokeRequired) {
// As before
} else {
// We're already on the UI thread just
// call straight through.
UpdateUI(this, new MyProgressEvents(msg,
PercentDone));
}
}

For more details, please refer to the MSDN magazine article:

Give Your .NET-based Application a Fast and Responsive UI with Multiple
Threads
http://msdn.microsoft.com/zh-cn/maga...29(en-us).aspx

Is the above information helpful to you? If you have any other questions or
concerns, please feel free to let me know.

Have a very nice day!

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 31 '08 #2
Jialiang

Thanks for a informative reply

I should have given more information re the type of Timer I use

I was referring to the System.Windows.Forms.Timer dragged from the toolbox

So you have answered my query nicely

Regards
Steve

""Jialiang Ge [MSFT]"" <ji****@online.microsoft.comwrote in message
news:xN*************@TK2MSFTNGHUB02.phx.gbl...
Good morning Steve,

Before we look at the two questions in the post, I'd like to first
introduce three different timer classes in the .NET Class Library:

System.Windows.Forms.Timer
System.Timers.Timer
System.Threading.Timer

The first two classes appear in the Visual Studio.NET toolbox window,
allowing us to drag and drop them directly onto a Windows Forms designer
or
a component class designer. System.Threading.Timer does not appear in the
toolbox, but it exposes several more advanced features. Alex Calvo [MSFT]
wrote a good article, going through the differences between the three in
detail.

Comparing the Timer Classes in the .NET Framework Class Library
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
>>Does the code in a Timer control.tick event run on a different thread
to the main Application thread (UI Thread)?

If the timer is the control dragged from the toolbox into the winform
designer, control.tick event handler would run on the same thread as the
UI
thread. It can be proved with this piece of code:

// in the winform, (e.g. Form_Load), we dump the UI thread ID:
Debug.Print("UI Thread: " +
Thread.CurrentThread.ManagedThreadId.ToString());

// in the timer's tick event handler, we dump the ID of the thread that
runs the handler:
private void timer1_Tick(object sender, EventArgs e)
{
Debug.Print("Tick: " +
Thread.CurrentThread.ManagedThreadId.ToString());
}
>>In some of the timers I update some UI controls e.g statusbar.labels
and I am wondering if I should be doing this if the code is running in
a different thread

If the timer belongs to System.Windows.Forms.Timer, or System.Timers.Timer
whose SynchronizingObject is set to the winform object (for example:

Dim tmrTimersTimer As New System.Timers.Timer();
tmrTimersTimer.SynchronizingObject = Me 'Synchronize with the current form

the timer's tick event will run in the UI thread, and we can directly
operate on the UI control (e.g. statusbar.labels).

However, if the timer belongs to System.Threading.Timer, or
System.Timers.Timer whose SynchronizingObject is NOT set to the winform
object, the ticket event will run in a different thread, and we need to
call either Control.Invoke or Control.BeginInvoke to operate on the UI
controls. For example:

// Created on UI thread
private Label lblStatus;

// Doesn't run on UI thread
private void RunsOnWorkerThread() {
DoSomethingSlow();
// Do UI update on UI thread
object[] pList = { this, System.EventArgs.Empty };
lblStatus.BeginInvoke(
new System.EventHandler(UpdateUI), pList);
}

// Code to be run back on the UI thread
// (using System.EventHandler signature
// so we don't need to define a new
// delegate type here)
private void UpdateUI(object o, System.EventArgs e) {
// Now OK - this method will be called via
// Control.Invoke, so we are allowed to do
// things to the UI.
lblStatus.Text = "Finished!";
}

For simplicity, we usually write a wrapper function, e.g

public void ShowProgress(string msg, int percentDone) {
if (InvokeRequired) {
// As before
} else {
// We're already on the UI thread just
// call straight through.
UpdateUI(this, new MyProgressEvents(msg,
PercentDone));
}
}

For more details, please refer to the MSDN magazine article:

Give Your .NET-based Application a Fast and Responsive UI with Multiple
Threads
http://msdn.microsoft.com/zh-cn/maga...29(en-us).aspx

Is the above information helpful to you? If you have any other questions
or
concerns, please feel free to let me know.

Have a very nice day!

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working
with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Oct 31 '08 #3
You are welcome, Steve.
Glad to help!

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================

Oct 31 '08 #4

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

Similar topics

3
1846
by: David | last post by:
Hi There! I'm using Timer control to record how long my application perform certain tasks. However, apparently Timer control is not doing its' job (i.e. Not firing Tick event) while my...
11
2546
by: Steve Jorgensen | last post by:
I've recently been playing with some UI ideas that require the user of a timer to drive animation. The problem I'm having is that Access routinely stops firing timer events for long periods of...
5
5074
by: Dhilip Kumar | last post by:
Hi all, I have developed a windows service using the windows service project template in VS.NET. I have used three controls in the service, a timer, performance counter and a message queue...
7
2365
by: Noozer | last post by:
I have a timer on a form. It isn't firing at all. I know that the timer is enabled, and that the interval is low (4000, which should be 4 seconds). To ensure the timer wasn't being inadvertantly...
8
2703
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
1
967
by: Neo | last post by:
Hi I have a SQL query that takes about 1.5 minutes to run after the click of a button. I also have a timer on the form that works perfectly indepentently. But once I combine them, them timer won't...
7
5947
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...
4
4617
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
5
4198
by: bobh | last post by:
Hi All, This should be simple enough but I'm not getting it working for some reason so, what's the code and in what events In AccessXP on a form when a user clicks on a button(btnGetData) I...
0
7265
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
7171
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
7388
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,...
1
7111
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
5692
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,...
0
3240
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...
0
1605
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 ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
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...

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.