设置进度条的最大值以匹配队列长度

本文关键字:队列 最大值 设置 | 更新日期: 2023-09-27 18:36:46

我想在 mainform 中的进度条的帮助下,可视化两个读取器线程和一个写入器线程访问队列时如何增长和收缩。我将使用委托在主窗体中调用进度条,并将其值设置为 Queue.Value ( Toys.lenght)。进度条的行为与所述不同,它不会一直增长,长度变量也不会。

     public class Buffer
     {
        private delegate void Display(int v, ProgressBar f );
        private Queue<Toy> Toys = new Queue<Toy>(); 
        private object MyLock = new object();
        private int max;
        private int lenght; 
        private ProgressBar progressbar1; 
        public Buffer(ProgressBar r)
        {
            this.progressbar1 = r; 
            this.max = 10;
            this.lenght = Toys.Count; 
        }
        public void writeMethod(Toy toy)
        {
            lock (MyLock)
            {
               if (Toys.Count == max)
               {
                   Monitor.Wait(MyLock); 
               }
               Toys.Enqueue(toy); 
               Monitor.PulseAll(MyLock);  
                progressbar1.Invoke(new Display(Disp), new object[] {Toys.Count, progressbar1});
                MessageBox.Show("Que contains these items" + lenght); 
            }
        }
        public void readMethod()
        {
            lock (MyLock)
            {
                if(Toys.Count == 0)
                {
                    Monitor.Wait(MyLock); 
                }
                Toys.Dequeue(); 
                Monitor.PulseAll(MyLock); 
            }
        }
        public void Disp(int I, ProgressBar l)
        {
            progressbar1.Value = I; 

        }
    }
  }

设置进度条的最大值以匹配队列长度

仔细检查变量长度,改用计数。将最大大小的进度条默认设置从 100 更改为 10,以匹配队列玩具的大小。在 Disp 方法中从 = I 更改为 += I;

   {
    public class Buffer
    {
        private delegate void Display(int v, ProgressBar f );
        private Queue<Toy> Toys = new Queue<Toy>(); 
        private object MyLock = new object();
        private int max;
        private int lenght; 
        private ProgressBar progressbar1; 
        public Buffer(ProgressBar r)
        {
            this.progressbar1 = r; 
            this.max = 10;
            this.lenght = Toys.Count; 
        }
        public void writeMethod(Toy toy)
        {
            lock (MyLock)
            {
               if (Toys.Count >= max)
               {
                   Monitor.Wait(MyLock);
               }
               if(Toys.Count <= max)
               {
                   Toys.Enqueue(toy); 
                  progressbar1.Invoke(new Display(Disp), new object[] {Toys.Count, progressbar1});
               }
                Monitor.PulseAll(MyLock);

                MessageBox.Show("Que contains these items" + Toys.Count); 
            }
        }
        public void readMethod()
        {
            lock (MyLock)
            {
                if(Toys.Count == 0)
                {
                    Monitor.Wait(MyLock); 
                }
                Toys.Dequeue(); 
                Monitor.PulseAll(MyLock); 
            }
        }
        public void Disp(int I, ProgressBar l)
        {
            progressbar1.Value += I; 
        }
    }
}