What is desired method vs timer for running continuous thread?

R

Rich P

I created a simple custom usercontrol that displays current time and
changes the color of a label control every second. I added a timer
control to my custom control and then added this to a winform app for
testing and for release. It works fine, but I think there is a more
desired way to run a custom control like this than using a timer.

My question is this - Is there is a better/more desired way to run a
clock in a user control than using a timer control? I am thinking like
tapping into the OS for the system clock or tapping into the network
server - I think I saw a sample like that but was not able to get it to
run.

Note: this project is mainly a training project for me.

Thanks in advance for any suggestions or sample code.

Rich
 
P

Peter Duniho

Rich said:
I created a simple custom usercontrol that displays current time and
changes the color of a label control every second. I added a timer
control to my custom control and then added this to a winform app for
testing and for release. It works fine, but I think there is a more
desired way to run a custom control like this than using a timer.

My question is this - Is there is a better/more desired way to run a
clock in a user control than using a timer control? [...]

What do you mean by "timer control"? Do you mean the
System.Windows.Forms.Timer object you can drag from the "Components"
section of the Designer's Toolbox in Visual Studio?

If so, and assuming you have just the one, I think that is probably the
single best choice for your particular implementation (though, to be
clear, it's not actually a "control"...it doesn't inherit the
System.Windows.Forms.Control class).

Because it's known to the Designer, it means you can do some of the
structural hookup in the Designer rather than having to explicitly write
code, and because it's the Timer object in the System.Windows.Forms
namespace, it has the definite advantage of being architected
specifically to work in a Forms application. In particular, your timer
tick events are always raised in the main GUI thread, so you don't wind
up have to do the cross-thread invocation work that would otherwise be
inevitable in a Forms application.

If that's not the timer you're talking about, it would be helpful in
answering your question if you would be more specific.

Pete
 
R

Rich P

Thanks for your reply. Yes, I am referring to the Timer control in the
VS toolbox (the thing that is shaped like an alarm clock). I think,
more specifically, I was concerned about the cross threading that you
mention in your reply. I have read articles referencing threading.timer
and system.Timer, and just wanted to sort this all out.

Rich
 
J

Jeff Johnson

Yes, I am referring to the Timer control in the
VS toolbox (the thing that is shaped like an alarm clock).

I mention this merely for the sake of mentioning this: I think it's really
supposed to look like a stopwatch.
 

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