Link Timer to a new thread

R

RP

I have a Timer control on a form. When this Timer starts, it will do
certain task on every tick. I want that this Timer be linked to a new
thread so that the Timer does not disturb other activities. How to
create a new thread and link this Timer to it?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

RP said:
I have a Timer control on a form. When this Timer starts, it will do
certain task on every tick. I want that this Timer be linked to a new
thread so that the Timer does not disturb other activities. How to
create a new thread and link this Timer to it?
You do not need to "link" the timer to the thread (or threads to be more
precise).
Just create a new thread in the Tick event.
In case ou need to have access to the UI from these threads use
Control.Invoke
 
P

Peter Duniho

RP said:
I have a Timer control on a form. When this Timer starts, it will do
certain task on every tick. I want that this Timer be linked to a new
thread so that the Timer does not disturb other activities. How to
create a new thread and link this Timer to it?

In addition to what Ignacio already wrote...

It seems to me that if you necessarily want the timer-signaled code to
execute on a different thread, then one of the Timer classes that
actually use a different thread for signaling the timer would be a
better solution than using the Forms.Timer class.

Forms.Timer is great for situations when you specifically want the
timer-signaled code executing on the main UI thread and the resolution
is sufficient for your needs. But it sounds like you want the opposite
(not executing on the main UI thread) and so using Forms.Timer is
counter-productive.

Also, if you still wind up for whatever reason assigning a different
thread to the code in response to the signaling of the timer, it would
probably be better to use a thread pool thread rather than creating a
whole new one. This is a general rule, not absolute advice. As the
timer interval is longer, and the code executed takes more time, the
overhead of creating a brand new thread is made relatively smaller and
isn't as big a problem.

Pete
 

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