473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Problem with System.Timers.Timer

Hi,

What is the best way to release all resources holded by the Timer
(myTimer from class System.Timers.Timer)?

Is it:
1- myTimer.dispose
2- myTimer.enabled = false
3- myTimer.close

I want to have the timer set to nothing so I can recreate a new one, I
want to do this in a second step:

myTimer = New System.Timers.Timer
AddHandler myTimer.Elapsed, AddressOf ListenToRequest
myTimer.Interval = 100
myTimer.Enabled = True

I am asking this because it seem that the timer loop a couple of time
before being stopped, and raising errors by the way.
This is my original code:
myTimer.Dispose()
myTimer = New System.Timers.Timer
AddHandler tmrClientQwery.Elapsed, AddressOf ListenToRequest
myTimer.Interval = 100
myTimer.Enabled = True

Thank you very much

Jul 21 '05 #1
3 2566
I normally set enabled to false first - so that I know that it will not kick again while I then dispose of it. AFAIK close just calls dispose - will have to look at the IL to be sure, however.

HTH
--
David Williams, VB.NET MVP
"User" wrote:
Hi,

What is the best way to release all resources holded by the Timer
(myTimer from class System.Timers.Timer)?

Is it:
1- myTimer.dispose
2- myTimer.enabled = false
3- myTimer.close

I want to have the timer set to nothing so I can recreate a new one, I
want to do this in a second step:

myTimer = New System.Timers.Timer
AddHandler myTimer.Elapsed, AddressOf ListenToRequest
myTimer.Interval = 100
myTimer.Enabled = True

I am asking this because it seem that the timer loop a couple of time
before being stopped, and raising errors by the way.
This is my original code:
myTimer.Dispose()
myTimer = New System.Timers.Timer
AddHandler tmrClientQwery.Elapsed, AddressOf ListenToRequest
myTimer.Interval = 100
myTimer.Enabled = True

Thank you very much

Jul 21 '05 #2
Thanks, I'll try to disable the timer first then dispose of it.

Have a good day.
David Williams wrote:
I normally set enabled to false first - so that I know that it will not kick again while I then dispose of it. AFAIK close just calls dispose - will have to look at the IL to be sure, however.

HTH


Jul 21 '05 #3
Hi User,

That happens because you set that timer to fire every 100 miliseconds. The
Timers.Timer fire Elapse event asyncron. So, and even if you can't see it
that timer fires more times that you think.

The best way to solve that is setting the AutoReset property to False and in
each Elapse event call Start method or set Enabled to True, at the end of
your handling code. Like this you garantee that only one instance of that
event fires at a time. And when you want to stop that timer just call Stop
method or set Enabled property to False.

If your handling code just take more that 100ms to process you will have
several instances of Elapse event handle code running at the same time. This
could be bad depending of what your are doing.

Be aware that the Timer has a bug that is mentioned 'Bug in Timers.Timer'.
Just use my workaround to handle it.

Pedro Gonçalves
"User" wrote:
Thanks, I'll try to disable the timer first then dispose of it.

Have a good day.
David Williams wrote:
I normally set enabled to false first - so that I know that it will not kick again while I then dispose of it. AFAIK close just calls dispose - will have to look at the IL to be sure, however.

HTH


Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Johnsson | last post by:
How come the eventhandler for the timer's elapsed time event is called over and over again, even though the AutoReset property is set to false, if you assign a new value to the timer objects...
2
by: DaveF | last post by:
I am trying to write a service to fire an ftp object off. I want to wait 2 minutes and then download the files. Then repeat every 2 minutes. The files just seem to stop downloading. Does that timer...
8
by: James A Taber | last post by:
What I want to implement is to restart certain thread every 24 hours... sample code: Dim T() As Thread Dim L() As Lib1.Class1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e...
10
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have...
3
by: Kris Palmer | last post by:
hi, can somebody explain this problem? it's driving me crazy! i have a requirement to dynamically create a variable quantity of timers with associated start button based on the contents of a...
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
2
by: zamir.khan | last post by:
Hello all, New to the groups, sorry if this the wrong forum/etiquette. I am coding a c++ application that requires the use of a timer-triggered event handler. I decided to use the timer provided...
0
by: qiangdi_he | last post by:
Hi, Pals, I am using WaitAny( ) to wait for the events triggered by a timer. But it seems like that when the timer's interval elapsed, and the event was set, but WaitAny is still waiting there....
6
by: Dave | last post by:
I have a service that has 6 different threads. Each thread has a timer on it that elapses at about the same time (if not the same time). When the timer elapses I am trying to log a message by...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.