0

In this multi-thread program I'm trying to remove two random elements from a list every third iteration of populating it. I thought I could do it by calculating the modulo of a "stepCounter" to keep track of how many times new elements were being added to the list (All the threads perform the same function) and after checking with a few breakpoints, I found that it is checking the stepCounter condition, then going to the first line of the "for" loop to pick two random indexes to remove.

The problem is that the removing is not actually being performed, as though it reads the "for i = etc. and then just skips over.

namespace ThreadWinApp { public partial class Form1 : Form { // thread variables is created here for the whole class private Thread trd; private Thread trd2; private Thread 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();   

    }

    }
}

        }

Is there something I'm missing? everything else seems to do what I want

4

1 に答える 1

1

forループの状態が間違っています。

チェックする必要がi <= 2あります。i == 2

于 2012-07-23T04:40:42.603 に答える