Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Self-deleting hashtable entries

Self-deleting hashtable entries

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi! I have a hashtable and add values from time to time like this:

    users.Add("Bob", 4);
    users.Add("John", 10);

    Now I want each entry I add into this list to be removed after a certain amount of time, e.g. 10 minutes. So I add "Bob", and Bob will be autoamtically removed from the hashtable after 10 minutes, same for John. What is the best way to do this? regards

    T T 2 Replies Last reply
    0
    • L Lost User

      Hi! I have a hashtable and add values from time to time like this:

      users.Add("Bob", 4);
      users.Add("John", 10);

      Now I want each entry I add into this list to be removed after a certain amount of time, e.g. 10 minutes. So I add "Bob", and Bob will be autoamtically removed from the hashtable after 10 minutes, same for John. What is the best way to do this? regards

      T Offline
      T Offline
      tylerl
      wrote on last edited by
      #2

      Run a clean-up function off a timer that scans the list for items older than 10 minutes, and deletes them. You'll have to keep track of how old an item is, of course. How you do that is really up to you, but I'd make the "value" portion of the table entry actually a class instance that holds your useful data plus a DateTime value.

      L 1 Reply Last reply
      0
      • L Lost User

        Hi! I have a hashtable and add values from time to time like this:

        users.Add("Bob", 4);
        users.Add("John", 10);

        Now I want each entry I add into this list to be removed after a certain amount of time, e.g. 10 minutes. So I add "Bob", and Bob will be autoamtically removed from the hashtable after 10 minutes, same for John. What is the best way to do this? regards

        T Offline
        T Offline
        tarasn
        wrote on last edited by
        #3

        I think the System.Web.Caching.Cache class is what are you looking for ... ;-) You can use it in WinForms application as shown below: using System.Threading; using System.Web; using System.Web.Caching; namespace WindowsApplication8 { /// /// Cache for Windows Forms /// public sealed class WinCache { private static HttpRuntime _httpRuntime; public static Cache Cache { get { EnsureHttpRuntime(); return HttpRuntime.Cache; } } private static void EnsureHttpRuntime() { if( null == _httpRuntime ) { try { Monitor.Enter( typeof( WinCache ) ); if( null == _httpRuntime ) { // Create an Http context to give us access to the cache. _httpRuntime = new HttpRuntime(); } } finally { Monitor.Exit( typeof( WinCache ) ); } } } } } Then you can easily insert the values into WinCache in following way: WinCache.Cache.Insert("Bob", 4, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); WinCache.Cache.Insert("John", 10, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); DevIntelligence.com - My blog for .Net Developers -- modified at 17:31 Tuesday 24th January, 2006

        D L 2 Replies Last reply
        0
        • T tarasn

          I think the System.Web.Caching.Cache class is what are you looking for ... ;-) You can use it in WinForms application as shown below: using System.Threading; using System.Web; using System.Web.Caching; namespace WindowsApplication8 { /// /// Cache for Windows Forms /// public sealed class WinCache { private static HttpRuntime _httpRuntime; public static Cache Cache { get { EnsureHttpRuntime(); return HttpRuntime.Cache; } } private static void EnsureHttpRuntime() { if( null == _httpRuntime ) { try { Monitor.Enter( typeof( WinCache ) ); if( null == _httpRuntime ) { // Create an Http context to give us access to the cache. _httpRuntime = new HttpRuntime(); } } finally { Monitor.Exit( typeof( WinCache ) ); } } } } } Then you can easily insert the values into WinCache in following way: WinCache.Cache.Insert("Bob", 4, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); WinCache.Cache.Insert("John", 10, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); DevIntelligence.com - My blog for .Net Developers -- modified at 17:31 Tuesday 24th January, 2006

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          tarasn wrote:

          I think the System.Web.Caching.Cache class is what are you looking for ... You can use it in WinForms application as shown below:

          I'm not sure if you should be applauded for code reuse or censored for abusing existing code into a place where it has no business being. (mainly because I don't know anything about the web classes)

          T 1 Reply Last reply
          0
          • D Dan Neely

            tarasn wrote:

            I think the System.Web.Caching.Cache class is what are you looking for ... You can use it in WinForms application as shown below:

            I'm not sure if you should be applauded for code reuse or censored for abusing existing code into a place where it has no business being. (mainly because I don't know anything about the web classes)

            T Offline
            T Offline
            tarasn
            wrote on last edited by
            #5

            I don't see any problem with my answer .You can create Console application and check the code that I sent .Instead reinventing the wheel ( and wasting the time ) you can use well implemented class .I highly recommend you read more on MSDN about Cache class - probably it solves your .Net problem too . :-D DevIntelligence.com - My blog for .Net Developers

            1 Reply Last reply
            0
            • T tarasn

              I think the System.Web.Caching.Cache class is what are you looking for ... ;-) You can use it in WinForms application as shown below: using System.Threading; using System.Web; using System.Web.Caching; namespace WindowsApplication8 { /// /// Cache for Windows Forms /// public sealed class WinCache { private static HttpRuntime _httpRuntime; public static Cache Cache { get { EnsureHttpRuntime(); return HttpRuntime.Cache; } } private static void EnsureHttpRuntime() { if( null == _httpRuntime ) { try { Monitor.Enter( typeof( WinCache ) ); if( null == _httpRuntime ) { // Create an Http context to give us access to the cache. _httpRuntime = new HttpRuntime(); } } finally { Monitor.Exit( typeof( WinCache ) ); } } } } } Then you can easily insert the values into WinCache in following way: WinCache.Cache.Insert("Bob", 4, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); WinCache.Cache.Insert("John", 10, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero); DevIntelligence.com - My blog for .Net Developers -- modified at 17:31 Tuesday 24th January, 2006

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              This looks cool, but what's all the web stuff in there? I don't plan on using any http capabilities in my console application (and thus no WinForms). regards

              T 1 Reply Last reply
              0
              • T tylerl

                Run a clean-up function off a timer that scans the list for items older than 10 minutes, and deletes them. You'll have to keep track of how old an item is, of course. How you do that is really up to you, but I'd make the "value" portion of the table entry actually a class instance that holds your useful data plus a DateTime value.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                It thought about that idea too, but found it a bit unflexible. But when there's no other way that's how I will do it. :) regards

                1 Reply Last reply
                0
                • L Lost User

                  This looks cool, but what's all the web stuff in there? I don't plan on using any http capabilities in my console application (and thus no WinForms). regards

                  T Offline
                  T Offline
                  tarasn
                  wrote on last edited by
                  #8

                  The WinCache class can be used in console application. DevIntelligence.com - My blog for .Net Developers

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups