User Timer in Class

  • Thread starter Thread starter Mathieu
  • Start date Start date
M

Mathieu

Hi all,

I just want to know how to use a System.Windows.Forms.Timer in class. I
know how to use it on a form, but I don't know how to "put" a timer in a
class (DLL)

Thx !!!
 
Mathieu said:
I just want to know how to use a System.Windows.Forms.Timer in
class. I know how to use it on a form, but I don't know how to "put"
a timer in a class (DLL)

There is no mystery behind. :-) The Form designer just generates one line
for you:

Private WithEvents MyTimer As New System.Windows.Forms.Timer

In other classes you can write this line on your own.

You might consider make your class a component, so the component designer
will be available and you can drop a Timer on the designer. Details:
http://msdn.microsoft.com/library/en-us/vbcon/html/vbconComponentAuthoring.asp
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcomponentprogrammingessentials.asp

Apart from this, why do you need such a timer in a class? The timer should
only be used in a UI (user interface) context.
 
Mathieu,

You can use this code in your class:

---------------------------
Private Withevents mTimer as New System.Windows.Forms.Timer

Private Sub TimerTickedHandler(sender as object, e as EventArgs) handles
mTimer.Tick
' Handle the timer tick
End Sub
---------------------------

You'll need to add code to your class to enable/disable the timer.

I suggest you have a look at the other timers (System.Timers.Timer and
System.Threading.Timer) as they provide a little bit more functionality than
the Windows Forms timer, but need a little bit of care when using with
windows forms as their events can be raised on different threads.

Hope this helps,

Trev.
 
Mathieu,
In addition to Armin's suggestion of how to do it.

Remember that to use System.Windows.Forms.Timer your application will need
the "message loop" running, which means it needs to have at least one form
running or you called System.Windows.Forms.Application.Run in your Sub Main.

If you are not otherwise using Windows Forms in your application you may
want to consider either the System.Threading.Timer class or the
System.Timers.Timer class.

Being a DLL it is hard to ensure that the main application will call
Application.Run!

Hope this helps
Jay
 
Hi Mathieu

In addition to all others

Try a time this, make a new program with only a timer from the toolbox on
it,
set the properties and open your code.

Look now at the code in Windows designer code (you can open that by pushing
the +)

That is the same as you can make and use on every form.

Than delete it and drag from the component box a timer (that is the
system.timers.timer) and try the same. That one you can use as far as I know
in any class.

But I have the same question as Armin, why would you make from this your own
DLL.
Or do you want to make your own Seiko's

I hope this helps,

Cor
 
Thx All
I will use System.Timers.Timer !!!


Codemonkey said:
Mathieu,

You can use this code in your class:

---------------------------
Private Withevents mTimer as New System.Windows.Forms.Timer

Private Sub TimerTickedHandler(sender as object, e as EventArgs) handles
mTimer.Tick
' Handle the timer tick
End Sub
---------------------------

You'll need to add code to your class to enable/disable the timer.

I suggest you have a look at the other timers (System.Timers.Timer and
System.Threading.Timer) as they provide a little bit more functionality than
the Windows Forms timer, but need a little bit of care when using with
windows forms as their events can be raised on different threads.

Hope this helps,

Trev.
 
I've used CCRP Timer and think it's brilliant for VB6 applications. Although
it can be used in DotNet via COM interop, there really isn't any need to use
it because of the System.Timers.Timer and System.Threading.Timer classes
which are better suited to the multithreaded-capable dotnet world.

Just my humble opinion.

Trev.
 
Just be careful if you are using the System.Timers.Timer timer when you are
dealing with Windows forms. Always be sure to call the Invoke() method of
the form if you want to call any other method on the form.

Trev
 
Back
Top