OdraEncoded

Untitled

Oct 28th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. private void CacheContinously()
  2. {
  3.     Monitor.Enter(this);
  4.  
  5.     while(Monitor.Wait(this) && _continouslyCaching) {
  6.         QueueEntry currentEntry;
  7.         while(GetNext(out currentEntry)) {
  8.             bool canSkip = _queueEntries.Count > 1;
  9.             Monitor.Exit(this);
  10.  
  11.             ICacheable cacheable = currentEntry.Cacheable;
  12.             bool toCache = currentEntry.ToCache;
  13.  
  14.             bool gotLock;
  15.             if(canSkip) {
  16.                 gotLock = false;
  17.                 Monitor.TryEnter(cacheable, ref gotLock);
  18.             } else {
  19.                 Monitor.Enter(cacheable);
  20.                 gotLock = true;
  21.             }
  22.  
  23.             if(gotLock) {
  24.                 if(toCache) {
  25.                     if(!cacheable.IsCached) {
  26. #if SPAM_CACHING_MESSAGES
  27.                         Console.WriteLine("Caching {0}...", cacheable);
  28. #endif
  29.                         cacheable.Cache();
  30.                         cacheCount++;
  31. #if SPAM_CACHING_MESSAGES
  32.                         Console.WriteLine("Cached {0}!", cacheable);
  33. #endif
  34.                     }
  35.                 } else if(cacheable.IsCached) {
  36. #if SPAM_CACHING_MESSAGES
  37.                     Console.WriteLine("Uncaching {0}...", cacheable);
  38. #endif
  39.                     cacheable.Uncache();
  40.                     cacheCount--;
  41. #if SPAM_CACHING_MESSAGES
  42.                     Console.WriteLine("Uncached {0}!", cacheable);
  43. #endif
  44.                 }
  45. #if SPAM_CACHING_MESSAGES
  46.                 Console.WriteLine("Total cached items: {0}!", cacheCount);
  47. #endif
  48.  
  49.                 Monitor.Exit(cacheable);
  50.             }
  51.  
  52.             Monitor.Enter(this);
  53.  
  54.             if(!gotLock) {
  55.                 if(!IsQueued(cacheable, !toCache)) {
  56.                     Enqueue(cacheable, toCache);
  57.                 }
  58.             }
  59.         }
  60.     }
  61.  
  62.     Monitor.Exit(this);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment