RE: Timer Control query



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 (jialge@xxxxxxxxxxxxxxxxxxxx, 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:
msdnmg@xxxxxxxxxxxxxx

==================================================
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.



.



Relevant Pages

  • Re: Timer Control query
    ... I was referring to the System.Windows.Forms.Timer dragged from the toolbox ... introduce three different timer classes in the .NET Class Library: ... Microsoft Online Community Support ... initial response from the community or a Microsoft Support Engineer within ...
    (microsoft.public.dotnet.languages.vb)
  • Re: One-shot high-resolution POSIX timer periodically late
    ... small bursts due to network jitter (typical average rate of 1000 packets ... Just before I re-send a packet, I arm a one-shot timer in order to ... Linux Plug and Play Support v0.97 Adam Belay ...
    (Linux-Kernel)
  • NForce2: 2.6.0-test11-bk6 UP high interrupt error count with lapic noapic
    ... It is necessary to have both the APIC timer and the 8254 timer running ... Can the 8254 timer be disabled if the APIC timer is running. ... ATAPI CD-ROM, with removable media ... Linux Plug and Play Support v0.97 Adam Belay ...
    (Linux-Kernel)
  • Re: ACPI PM Timer vs. C1 halt issue
    ... ACPI as such is included. ... hot again even with tsc timer. ... # ACPI Support ... # Old CD-ROM drivers ...
    (Linux-Kernel)
  • Re: Executing a method in a given thread context
    ... What is the impact of using P/Invoke? ... control's UI thread without Timer class user aware of. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.clr)

Loading