locked
System.IndexOutOfRangeException was unhandled RRS feed

  • Question

  • What do you make of it?

    System.IndexOutOfRangeException was unhandled
      Message="Index was outside the bounds of the array."
      Source="mscorlib"
      StackTrace:
           at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices)
           at System.Array.SetValue(Object value, Int32 index)
           at System.Collections.Hashtable.CopyValues(Array array, Int32 arrayIndex)
           at System.Windows.Forms.Application.ThreadContext.ExitCommon(Boolean disposing)
           at System.Windows.Forms.Application.ExitInternal()
           at System.Windows.Forms.Application.Exit(CancelEventArgs e)
           at tapTCPports_1.Form1.pushSTOP_Click(Object sender, EventArgs ee) in C:\VCSharp_Projects\tapTCPports_1\Form1.cs:line 783
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at tapTCPports_1.Form1.Main() in C:\VCSharp_Projects\tapTCPports_1\Form1.cs:line 2241
      InnerException:

    And the place is....

    TSP.textToSpeech ( "Goodby" );
                Process[ ] pros = System.Diagnostics.Process.GetProcesses ( );
                foreach ( Process pro in pros )
                {
                    if ( pro.ToString ( ).IndexOf ( "(getMessageForm)" ) != -1 )
                    {
                        pro.Kill ( );
                        break;
                    }
                }
                Application.Exit ( );  // <<==== Exception
            }                                               // pushSTOP_Click


    AlexB
    Thursday, September 3, 2009 3:08 PM

Answers

  • If you try to remove an object from the list that you're foreaching over it will throw an error. But in this case it can't because it doesn't know that pro.Kill() removes the process from the list. Basically you can't use pro.Kill in a foreach. I would suggest using a for loop in reverse order instead of a foreach.
    Thursday, September 3, 2009 3:12 PM

All replies

  • If you try to remove an object from the list that you're foreaching over it will throw an error. But in this case it can't because it doesn't know that pro.Kill() removes the process from the list. Basically you can't use pro.Kill in a foreach. I would suggest using a for loop in reverse order instead of a foreach.
    Thursday, September 3, 2009 3:12 PM
  • If you try to remove an object from the list that you're foreaching over it will throw an error. But in this case it can't because it doesn't know that pro.Kill() removes the process from the list. Basically you can't use pro.Kill in a foreach. I would suggest using a for loop in reverse order instead of a foreach.

    It is a very good point. Overlooked:) I usually use for (... in reverse in case there ae a few of them.
    AlexB
    Thursday, September 3, 2009 6:14 PM