• 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.
  • Posts

    • Here in Finland we have lots of rural areas with narrow roads, one of the highest taxes in the world (cars are taxed way above EU standards) and fuel is quite expensive. Yet we educate our drivers to drive responsibly and safely in all areas, and our people respect each others rights and freedom to move around safely. Which is why we have even small children under 10 years old walking and cycling alone to schools in the streets, even in big cities. Safety is about being responsible and about respecting others. And I would hate to see AI (or anyone else) destroy our way of life. There are an always will be always outliers, and accidents happen, and machines break. I dont't want to see people relying on AI to do things like driving for them. I want people to think and react to the world around them themselves, and being responsible instead of them browsing TikTok or whatever instead of looking out the window, and then saying that "It wasn't me, it was the car". Already people walk around town with their eyes glued to a screen – I don't want people driving around the same way.
    • And I should hope none will. I don't want to walk ouside to have some randome AI drive over me and mine. Not that I want a person to do it either but I want there to be an actual person who takes responsibility of their actions instead of relinquishing control to a machine. In some highways I can accept self-driving, but even then there should be some kind of dead man switch etc. that actually monitors the drivers status.
    • No thank you. I want to drive myself, and not just because I don't trust a machine or whatever – I like driving, and I like doing it with a manual car without lane guidance and all other "driver assists". I wven rarely use cruise control. I went through and paid for to have a license to that allows me to do it, and do it responsibly instead of relinquishing control to a machine. I currently drive a van for work in a city, something like 200 km/day. If we everything is automated and computers decide everything for us, the dumber people will get since they don't have to bother thinking for themselves nor do they have to take responsibility for themselves since "it wasn't me, it was the machine" will be their future defence for everything. Soon, Neowin's writers will be out of a job because AI can do it just fine and it doesn't need pay. Wikipedia – a free service with voluntary writers – just started replacing real people with AI, and had to shut it down (at least for now). Lets let ai AI and those that run it (or rather run them) the keys the the world and watch it burn because no one is able to actually do anything without some AI assistant telling them what to do (driving included). What a world!
    • I’m sure in the last 12 months there’s been more air disasters
    • And it will get even wors the more automation and other features limiting driver interaction there are in never cars. The same also applies to many other things these days than driving. Let's make people dumber by not educating them and building things in a way where they do everything for people! Welcome to the future, where you don't have to think for yourself! Welcome to idiocrazy!
  • Recent Achievements

    • One Month Later
      POR2GAL4EVER earned a badge
      One Month Later
    • One Year In
      Orpheus13 earned a badge
      One Year In
    • One Month Later
      Orpheus13 earned a badge
      One Month Later
    • Week One Done
      Orpheus13 earned a badge
      Week One Done
    • Week One Done
      serfegyed earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      535
    2. 2
      ATLien_0
      246
    3. 3
      +FloatingFatMan
      176
    4. 4
      +Edouard
      166
    5. 5
      Xenon
      119
  • Tell a friend

    Love Neowin? Tell a friend!