Advertisement
CorrM

LimitedReader

Sep 9th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. public class LimitedReader
  2.     {
  3.         private Queue<string> List { get; set; } = new Queue<string>();
  4.         private uint LinesChank { get; set; } = 1000;
  5.         private string FilePath { get; set; }
  6.         public bool ReadFinished { get; internal set; } = false;
  7.         public bool IsEmpty { get; internal set; } = false;
  8.         public int ReadedLines { get; internal set; }
  9.         public bool Waiting { get; set; } = false;
  10.         public object lockobj { get; set; } = new object();
  11.  
  12.         private bool bWait = false;
  13.         private bool Working = false;
  14.  
  15.         public LimitedReader(string FilePath, uint LinesChank)
  16.         {
  17.             this.LinesChank = LinesChank;
  18.             this.FilePath = FilePath;
  19.         }
  20.  
  21.         public void Start()
  22.         {
  23.             if (Working)
  24.                 return;
  25.  
  26.             Working = true;
  27.             new Thread(() =>
  28.             {
  29.                 foreach (string line in File.ReadLines(FilePath))
  30.                 {
  31.                     // Check Wait State,, And wait :)
  32.                     while (bWait)
  33.                     {
  34.                         bWait = true;
  35.                         Thread.Sleep(50);
  36.                     }
  37.  
  38.                     // Check If Stopped
  39.                     if (ReadFinished)
  40.                         return;
  41.  
  42.                     // Check If reach the limit
  43.                     while (List.Count >= LinesChank)
  44.                     {
  45.                         // Check If Stopped
  46.                         if (ReadFinished)
  47.                             return;
  48.  
  49.                         Waiting = true;
  50.                         Thread.Sleep(50);
  51.                     }
  52.                     Waiting = false;
  53.  
  54.                     // Add To Queue
  55.                     lock (lockobj)
  56.                     {
  57.                         List.Enqueue(line);
  58.                         ReadedLines++;
  59.                     }
  60.  
  61.                     Thread.Sleep(1);
  62.                 }
  63.                 ReadFinished = true;
  64.             })
  65.             { IsBackground = true, Priority = ThreadPriority.AboveNormal }.Start();
  66.         }
  67.  
  68.         public string Dequeue()
  69.         {
  70.         ReTry:
  71.             try
  72.             {
  73.                 lock (lockobj)
  74.                 {
  75.                     string ret = List.Dequeue();
  76.                     return ret;
  77.                 }
  78.             }
  79.             catch
  80.             {
  81.                 if (ReadFinished)
  82.                 {
  83.                     Thread.Sleep(500);
  84.                     if (List.Count == 0)
  85.                     {
  86.                         IsEmpty = true;
  87.                         return "NULL";
  88.                     }
  89.                 }
  90.                 Thread.Sleep(1);
  91.                 goto ReTry;
  92.             }
  93.         }
  94.  
  95.         public void Stop()
  96.         {
  97.             bWait = true;
  98.             ReadFinished = true;
  99.         }
  100.  
  101.         public void Wait()
  102.         {
  103.             bWait = true;
  104.         }
  105.  
  106.         public void Continue()
  107.         {
  108.             bWait = false;
  109.         }
  110.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement