-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