我有一个DoWork
事件backgroundworker
这个代码:为什么我得到异常异常?
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
WaitHandle[] waitHandleList = null;
List<ManualResetEvent> mre = new List<ManualResetEvent>();
for (int i = 0; i < GetProcessesIntptrList().Count; i++)
{
ProcessInfo info;
info = GetProcessInfo(GetProcessesIntptrList()[i]);
if (procList.Count == GetProcessesIntptrList().Count)
break;
}
for (int i = 0; i < procList.Count; i++)
{
if (i == 9)
{
break;
}
else
{
SafeWaitHandle swh1 = new SafeWaitHandle(procList[i].Handle, false);
mre[i] = new ManualResetEvent(true);
mre[i].SafeWaitHandle = swh1;
waitHandleList = new WaitHandle[] { mre[i] };
worker.ReportProgress(0, "Running");
}
}
while (true)
{
if (procList != null)
{
for (int i = 0; i < procList.Count; i++)
{
WaitHandle.WaitAll(waitHandleList);
if (procList[i].HasExited)
{
worker.ReportProgress(0, "Exited");
procList[i] = null;
}
}
}
if ((worker.CancellationPending == true))
{
e.Cancel = true;
}
else
{
}
}
}
就行了:
mre[i] = new ManualResetEvent(true);
System.ArgumentOutOfRangeException
类型的异常出现在mscorlib.dll
但在用户代码中没有处理
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
当发生异常时mre为空count 0和I is als 0 0 问题是如何创建9个mre的实例?
当您第一次尝试访问它时,它是空的。 – deathismyfriend
如果我是0,那么它将尝试访问mre中的第一个元素...如果它为空,则不存在。所以它超出范围。 – User92