簡體   English   中英

我有“索引超出數組范圍”錯誤

[英]I have "Index was outside the bounds of the array" error

我在單擊 button3 和 button4 時遇到問題我不知道我做錯了什么看起來我的程序試圖將 threadArray 置於索引之外的位置

ps我的英語不好,但我讀得很好

    Thread[] threadArray;
    int numberThread;

    public delegate void myDelegate(int x);
    myDelegate[] myDelegates;

    int[] numberOfIterations;

    public Form1()
    {
        InitializeComponent();
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox1.SelectedIndex = 0;
        comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox2.Enabled = false;
        comboBox3.Enabled = false;
        numericUpDown2.Enabled = false;
        comboBox3.Items.Add("product");
        comboBox3.SelectedIndex = 0;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        threadArray = new Thread[(int)numericUpDown1.Value];
        numberThread = (int)numericUpDown1.Value;
        myDelegates = new myDelegate[(int)numericUpDown1.Value];
        numericUpDown1.Enabled = false;
        numberOfIterations = new int[numberThread];
        for (int i = 0; i < numberThread; i++)
        {
            comboBox2.Items.Add(i+1);
        }
        comboBox2.Enabled = true;
        button1.Enabled = false;
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBox3.Enabled = true;
        numericUpDown2.Enabled = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        int x = Convert.ToInt32(comboBox2.SelectedItem);
        x = x - 1;
        numberOfIterations[x] = (int)numericUpDown2.Value;
        if (comboBox3.SelectedIndex == 0)
        {
            myDelegates[x] = null;
            myDelegates[x] += Functions.productDev;
        }
        threadArray[x] = new Thread(()=>myDelegates[x]((int)numericUpDown2.Value));
    }

    private void button4_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < numberThread; i++)
        {
            threadArray[i].Start();
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        for (int j = 0; j < numberThread; j++)
        {
            numberOfIterations[j] = (int)numericUpDown2.Value;
            if (comboBox3.SelectedIndex == 0)
            {
                myDelegates[j] = null;
                myDelegates[j] += Functions.productDev;
            }
            threadArray[j] = new Thread(() => myDelegates[j](numberOfIterations[j]));
        }
    }

}

}

我認為問題在於您的所有數組都在button1_Click事件中初始化。 如果您沒有按下Button1怎樣。 而是在構造函數中初始化它們。

public void InitValues()
{
    threadArray = new Thread[(int)numericUpDown1.Value];
    numberThread = (int)numericUpDown1.Value;
    myDelegates = new myDelegate[(int)numericUpDown1.Value];
    numericUpDown1.Enabled = false;
    numberOfIterations = new int[numberThread];
}

在構造函數中調用此函數並從Button1_Click事件中刪除此代碼。

public Form1()
{
    //..............
    InitValues();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM