Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Starting and stopping a Threading.Timer
Message
 
À
01/10/2008 18:27:13
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Divers
Thread ID:
01352099
Message ID:
01352164
Vues:
24
>>The only thing different for me is I don't want to dispose the timer when I stop it. Because I have a bit of processing to do on the timer interval, I want to suspend the timer while I do the processing and then start it back up again when I am done. I also may need to adjust the delay time if the previous service access wasn't successful so there is more time elapsed before trying again. The best I have come up with is to do a timer.Change(Timeout.Infinite, this.QCheckInterval); and then set a new time again after the processing is complete.
>>I like your startup and stop methods as they are pretty clean.
>>Tim
>
>I tried to make this flexible for either starting or adjusting and also to handle both of my timers but not sure how to handle the Delegate method yet.

Sorry, not sure what you mean. If you want to pass in the delegate, you can do that with something like this:
        private void SetTimer(System.Threading.Timer timer, int delaySeconds, int intervalSeconds, TimerCallback callback)
        {
            if (timer == null)
            {
                using (AutoResetEvent autoEvent = new AutoResetEvent(true))
                {
                    TimeSpan delayTime = new TimeSpan(0, 0, 0, delaySeconds, 0);
                    TimeSpan timerInterval = new TimeSpan(0, 0, 0, intervalSeconds, 0);

                    timer = new System.Threading.Timer(callback, autoEvent,
            delayTime, timerInterval);
                }
            }
            else // Reset Timer
            {
                timer.Change(delaySeconds, intervalSeconds);
            }
        }
Then just add a few overloads to remove the unnecessary parameters.
-Paul

RCS Solutions, Inc.
Blog
Twitter
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform