• 0

[C#] Timers with Thread Pool


Question

Hi guys, firstly I got some Timers setup to run against the Thread pool using the TimerCallback right? Basically what I want is I want the Timer to run every x amount of seconds but only run if an event happens, I did look at manual reset event and stuff but im not sure how it works.

So say there are 1 items in a dictionary, the timer runs at x amount of seconds against thread pool, but if the dictionary is empty, the timer just sits there and doesn't queue any work load but continues again once the dictionary is populated, help appreciated :D

matt.

Link to comment
https://www.neowin.net/forum/topic/1018984-c-timers-with-thread-pool/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

do a function to check if dictionary is empty and if true u set timer1.enabled = true and if false timer1.enabled=false .. the X seconds of timer can be edited on component proprietires on design window

oh and the function to check if dictionary is empty must be a infinite loop, a thread, something like that

oh and the function to check if dictionary is empty must be a infinite loop, a thread, something like that

  • 0
  On 16/08/2011 at 15:42, guitmz said:

do a function to check if dictionary is empty and if true u set timer1.enabled = true and if false timer1.enabled=false .. the X seconds of timer can be edited on component proprietires on design window

oh and the function to check if dictionary is empty must be a infinite loop, a thread, something like that

oh and the function to check if dictionary is empty must be a infinite loop, a thread, something like that

You can't do enabled or disabled on a Threading Timer!

  • 0

I hate it when people don't actualy try to answer the question so I will do that. Can I just say first that a timer waking up every few seconds, not doing any work because the dictionary is empty and then going back to sleep is a miniscule use of resource so you really shouldn't worry.

Anyhow lets say you really want to do this.

Set the timer up to only do this once. When you create your timer you can spicify delay until start and timer delay (this part is what wakes it up and calls the callback after the first call).

Do this

Create timer but not to run and set the callback timer to infinite. Timer timer = new Timer(Time.Infinite, Time.Infinite, CallBack);

Inside your method that does some work when the timer is called back. If the dictionary has values Change the timer to start in a few seconds and the wake up time to infinite. timer.Change(3000,Time.Infinite);

Now we have control over the timers callback. If the Dictionary was empty the timer has ended and won't call again until something does the Change.

In our code when we add something to the Dictionary we can check some bool value to see if the Timer is set if not Change it so that the start delay is our period and the callback delay is infinite this start the process again.

public void AddToDictionary(Key key, Value value)

{

myDic.Add(key, value);

if(timerNotSet)

{

timer.Change(0, Time.Infinite);

}

}

In this way if that dictionary spends most of its time empty the timer will spend most of it's time dormant.

FYI when you are really finished with the Timer make sure you Dispose it.

guitmz Dude I have no idea what to say about your post above. So your saying run an infinte loop on a thread to see if a timer is to be switched on or off. No I have thought about it and there is just nothing to say about that.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.