Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LimitedReader
- {
- private Queue<string> List { get; set; } = new Queue<string>();
- private uint LinesChank { get; set; } = 1000;
- private string FilePath { get; set; }
- public bool ReadFinished { get; internal set; } = false;
- public bool IsEmpty { get; internal set; } = false;
- public int ReadedLines { get; internal set; }
- public bool Waiting { get; set; } = false;
- public object lockobj { get; set; } = new object();
- private bool bWait = false;
- private bool Working = false;
- public LimitedReader(string FilePath, uint LinesChank)
- {
- this.LinesChank = LinesChank;
- this.FilePath = FilePath;
- }
- public void Start()
- {
- if (Working)
- return;
- Working = true;
- new Thread(() =>
- {
- foreach (string line in File.ReadLines(FilePath))
- {
- // Check Wait State,, And wait :)
- while (bWait)
- {
- bWait = true;
- Thread.Sleep(50);
- }
- // Check If Stopped
- if (ReadFinished)
- return;
- // Check If reach the limit
- while (List.Count >= LinesChank)
- {
- // Check If Stopped
- if (ReadFinished)
- return;
- Waiting = true;
- Thread.Sleep(50);
- }
- Waiting = false;
- // Add To Queue
- lock (lockobj)
- {
- List.Enqueue(line);
- ReadedLines++;
- }
- Thread.Sleep(1);
- }
- ReadFinished = true;
- })
- { IsBackground = true, Priority = ThreadPriority.AboveNormal }.Start();
- }
- public string Dequeue()
- {
- ReTry:
- try
- {
- lock (lockobj)
- {
- string ret = List.Dequeue();
- return ret;
- }
- }
- catch
- {
- if (ReadFinished)
- {
- Thread.Sleep(500);
- if (List.Count == 0)
- {
- IsEmpty = true;
- return "NULL";
- }
- }
- Thread.Sleep(1);
- goto ReTry;
- }
- }
- public void Stop()
- {
- bWait = true;
- ReadFinished = true;
- }
- public void Wait()
- {
- bWait = true;
- }
- public void Continue()
- {
- bWait = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement