Timer Control query

S

Steve

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
 
J

Jialiang Ge [MSFT]

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/magazine/cc300429(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 ([email protected], 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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#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/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steve

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]" said:
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/magazine/cc300429(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 ([email protected], 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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#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/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
J

Jialiang Ge [MSFT]

You are welcome, Steve.
Glad to help!

Regards,
Jialiang Ge ([email protected], 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:
(e-mail address removed).

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

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