473,748 Members | 8,779 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.label s 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 3881
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.T imer
System.Threadin g.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.Threadin g.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.CurrentT hread.ManagedTh readId.ToString ());

// in the timer's tick event handler, we dump the ID of the thread that
runs the handler:
private void timer1_Tick(obj ect sender, EventArgs e)
{
Debug.Print("Ti ck: " +
Thread.CurrentT hread.ManagedTh readId.ToString ());
}
>In some of the timers I update some UI controls e.g statusbar.label s
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.T imer
whose SynchronizingOb ject is set to the winform object (for example:

Dim tmrTimersTimer As New System.Timers.T imer();
tmrTimersTimer. SynchronizingOb ject = 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.label s).

However, if the timer belongs to System.Threadin g.Timer, or
System.Timers.T imer whose SynchronizingOb ject 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.BeginIn voke to operate on the UI
controls. For example:

// Created on UI thread
private Label lblStatus;

// Doesn't run on UI thread
private void RunsOnWorkerThr ead() {
DoSomethingSlow ();
// Do UI update on UI thread
object[] pList = { this, System.EventArg s.Empty };
lblStatus.Begin Invoke(
new System.EventHan dler(UpdateUI), pList);
}

// Code to be run back on the UI thread
// (using System.EventHan dler signature
// so we don't need to define a new
// delegate type here)
private void UpdateUI(object o, System.EventArg s 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(st ring msg, int percentDone) {
if (InvokeRequired ) {
// As before
} else {
// We're already on the UI thread just
// call straight through.
UpdateUI(this, new MyProgressEvent s(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****@microsof t.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.comwr ote in message
news:xN******** *****@TK2MSFTNG HUB02.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.T imer
System.Threadin g.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.Threadin g.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.CurrentT hread.ManagedTh readId.ToString ());

// in the timer's tick event handler, we dump the ID of the thread that
runs the handler:
private void timer1_Tick(obj ect sender, EventArgs e)
{
Debug.Print("Ti ck: " +
Thread.CurrentT hread.ManagedTh readId.ToString ());
}
>>In some of the timers I update some UI controls e.g statusbar.label s
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.T imer
whose SynchronizingOb ject is set to the winform object (for example:

Dim tmrTimersTimer As New System.Timers.T imer();
tmrTimersTimer. SynchronizingOb ject = 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.label s).

However, if the timer belongs to System.Threadin g.Timer, or
System.Timers.T imer whose SynchronizingOb ject 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.BeginIn voke to operate on the UI
controls. For example:

// Created on UI thread
private Label lblStatus;

// Doesn't run on UI thread
private void RunsOnWorkerThr ead() {
DoSomethingSlow ();
// Do UI update on UI thread
object[] pList = { this, System.EventArg s.Empty };
lblStatus.Begin Invoke(
new System.EventHan dler(UpdateUI), pList);
}

// Code to be run back on the UI thread
// (using System.EventHan dler signature
// so we don't need to define a new
// delegate type here)
private void UpdateUI(object o, System.EventArg s 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(st ring msg, int percentDone) {
if (InvokeRequired ) {
// As before
} else {
// We're already on the UI thread just
// call straight through.
UpdateUI(this, new MyProgressEvent s(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****@microsof t.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****@microsof t.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
1867
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 application is busy. So even if my application took 2 mins, the label that is used to show the number of seconds elapsed will still say "2 seconds".
11
2566
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 time. For example, the user types a character in another window, and the timer stops. The user types another key, and the timer starts again, runs for a few seconds, then stops again. Now, the code I'm writing would easily tolerate a few...
5
5091
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 control. The service will "sleep" for 'n' seconds using the timer control and whenever the timer_elapsed event occurs, I use the performance counter object to determine availability of few resources. Based on the availability of resources, I use the...
7
2381
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 reset I put some extra code in the subs that enable and disable the timer. They fire as expected. To test this I added a second timer with a 1 second interval. The event for this time would output the enabled status of the first timer and its...
8
2735
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 thread to access the database and then displays a modal dialog which allows the user to cancel the task, if it is taking longer than they want, and shows them a display of how long the query has been running so far.
1
975
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 receive its event until the sql query is completed, which makes it totally useless. Is there a way that I can program around this. Do I have to make a multi threading environment?? Thanks
7
6071
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 seems recently connections just drop off because the timer stops firing. My question is if there is a timeout in the timer event that just shuts down the call if the timer event is taking too long to complete...?
4
4637
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 program is like so: static void Main() {
5
4212
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 want a labelbox's text(lblWait) to bold/unbold every second until the form closes. The vba behind the btnGetData button gets data and updates a table and then closes its self. The data it gets and updates could take several
0
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9319
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
9243
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
8241
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
6795
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
4599
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
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.