2012-07-23 29 views
0

在这个多线程程序中,我试图从列表中删除两个随机元素每填充它的第三次迭代。我认为我可以通过计算“stepCounter”的模数来跟踪新元素被添加到列表中的次数(所有线程执行相同的功能),并在检查了几个断点后,我发现它正在检查stepCounter条件,然后转到“for”循环的第一行以选择两个要删除的随机索引。for循环越过,列表对象不被删除在Visual C#

的问题是,除去没有实际执行,就好像它读取“对于i =等等,然后就跳过

命名空间ThreadWinApp { 公共部分Form1类:表格{ 在这里创建 //线程变量全班 私人主题TRD; 私人主题TRD2; 私人主题TRD3;

//this locker object is used to sychronize the threads 
    private System.Object locker = new System.Object(); 

    //random value object for use in all threads 
    Random random = new Random(); 
    public int val; 
    private int stepCounter = 0; 

    //our "array" of integers undefined in size 
    private List<int> numList = new List<int>(); 
    private int[] arry; 



    private void ThreadTask() 
    { 

     //this the thread that will at random intervals add a random number to the collection. 
    // Every third iteration, the thread should remove two random 
    // elements from the collection. 
     int randomVal; 
     int randomListVal; 
     // int stepCounter = 0; 


     while (true) 
     { 
      lock (locker) 
      { 
       randomVal = random.Next(0, 20); 
       numList.Add(randomVal); 
       stepCounter += 1; 

       if (((stepCounter % 3) == 0)) 

        for (int i = 0; i == 2; i++) 
        { 
        randomListVal = random.Next(0, numList.Count); 
        numList.RemoveAt(randomListVal); 

        } 

       Thread.Sleep(randomVal * 100);    
      } 

     } 

     } 
    private void ThreadTask2() 
    { 

     //this the thread that will read and show data from the array created in ThreadTask2() 
     // at random intervals add a random number to the collection. 
     // Every third iteration, the thread should remove two random 
     // elements from the collection. 

     int randomVal; 
     int randomListVal; 
    // int stepCounter = 0; 

     while (true) 
     { 
      lock (locker) 
      { 
       randomVal = random.Next(0, 20); 
       numList.Add(randomVal); 
       stepCounter += 1; 

       if (stepCounter % 3 == 0) 
       { 
        for (int i = 0; i == 2; i++) 
        { 
         randomListVal = random.Next(0, numList.Count); 
         numList.RemoveAt(randomListVal); 
        } 
       } 

       Thread.Sleep(randomVal * 100); 

      } 
     } 
    } 

     private void ThreadTask3() 
    { 

     //this the thread that will read and show data from the array created in ThreadTask2() 
    // at random intervals add a random number to the collection. 
    // Every third iteration, the thread should remove two random 
    // elements from the collection. 
     int randomVal; 
     int randomListVal; 




     while (true) 
     { 
      lock (locker) 
      { 

       randomVal = random.Next(0, 20); 
       numList.Add(randomVal); 
       stepCounter += 1; 



       if (stepCounter % 3 == 0) 
       { 
        for (int i = 0; i == 2; i++) 
        { 
         randomListVal = random.Next(0, numList.Count); 
         numList.RemoveAt(randomListVal); 
        } 
       } 

       Thread.Sleep(randomVal * 100); 

      } 

     } 
     } 


     private void ThreadTask4() 
     { 
      int[] array; 

      while (true) 
      { 
       lock (locker) 
       { 

        array = numList.ToArray(); 
        for (int i = 0; i < array.Length; i++) 
        { 
         textBox1.Text += (array[i] + " "); 
        } 

        Thread.Sleep(1000); 
        textBox1.Text += ("\r\n"); 

       } 

      } 
     } 
    private void Form1_Load(object sender, EventArgs e) 
    { 

     //creates a new instance of thread1 
     Thread trd = new Thread(new ThreadStart(this.ThreadTask)); 
     //makes the new thread a background thread, allowing easy termination 
     //upon closing of the application 
     trd.IsBackground = true; 
     //starts the thread 
     trd.Start(); 


     /*Same process as above, but for thread2*/ 
     Thread trd2 = new Thread(new ThreadStart(this.ThreadTask2)); 
     trd2.IsBackground = true; 
     trd2.Start(); 

     Thread trd3 = new Thread(new ThreadStart(this.ThreadTask3)); 
     trd3.IsBackground = true; 
     trd3.Start(); 


     Thread trd4 = new Thread(new ThreadStart(this.ThreadTask4)); 
     trd4.IsBackground = true; 
     trd4.Start(); 

    } 

    } 
} 

     } 

我有没有我失踪的东西?一切似乎都做我想要的

回答

1

您的for循环中的条件是错误的。

您应该检查i <= 2,而不是i == 2

+0

这奏效了,谢谢! – user1544919 2012-07-23 12:55:53