2015-01-07 85 views
-1

我想通過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; 


     } 
    } 
    } 
+0

不知道你的問題是什麼。進度條的最小值和最大值是多少?最重要的是,你是否曾經爲「玩具」添加過多項目? –

+0

我從來沒有設法添加任何東西,但進度條增長約10%的酒吧。我首先想到的是progressbar migh有一些我需要調整的設置,例如:將條的長度設置爲100,並且在將一個項添加到隊列中時應該填充10%。這有意義嗎? – AV67

+0

我的水晶球說最大= 10,progressbar1.Maximum = 100.狂野不適當的用戶界面順便說一句,這個值越大,你取得的進展越少。 –

回答

0

仔細檢查變量長度,用count代替。將進度欄默認設置從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; 

     } 
    } 
}