-2
我有問題,當我點擊button3和button4 我沒有想法我做錯了 它看起來我的程序試圖獲得threadArray的位置,除指數的我有「索引超出數組的界限」錯誤
PS我的英語isn`t不錯,但我可以閱讀很好
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]));
}
}
}
}
這意味着你正試圖訪問超過其長度的位置上的數組元素。另外,你需要解釋你想要達到什麼目的以及發生了什麼問題。不要指望你會粘貼一段代碼,我們將開始尋找你的問題。 – Tarec
你爲什麼不嘗試附加調試器? – bit
button3_Click生成threadArray,其中lenght等於numberThread該數組的所有元素都已經加入委託,然後當我點擊button4時,它嘗試調用所有threadarray元素併發生該錯誤 – user3411276