0

我正在尝试非常快速地多次更改状态条上的标签图像。我收到 ArgumentOutOfRangeException。我已经在标签和状态条以及 OnPaint 覆盖的绘制事件中添加了捕获,但我未能捕获它。例外总是伴随着一个红十字代替状态条。

这是更改图像的代码。每次从串行连接接收数据时都会调用它。目标是在接收到数据时闪烁灯。图像为 16 像素 x 16 像素。

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    try
    {
        if (ss_RD.Image != SerialScripter.Properties.Resources.GreenOrb)
            ss_RD.Image = SerialScripter.Properties.Resources.GreenOrb;

         SerialPort sp = (SerialPort)sender;
         string indata;

         int bytes = serialConnection.BytesToRead;
         switch (tBtn_receiveAsHex.Checked)
         {
             case false:
                 char[] charBuffer = new char[bytes];
                 serialConnection.Read(charBuffer, 0, bytes);
                 indata = CharToAscii(charBuffer);
                 break;
             case true:
                 byte[] byteBuffer = new byte[bytes];
                 serialConnection.Read(byteBuffer, 0, bytes);
                 indata = ByteToHex(byteBuffer);
                 break;
             default:
                 indata = sp.ReadExisting();
                 break;
        }

        this.BeginInvoke(new SetTextDeleg(DisplayToUI), new object[] { indata });

        if (ss_RD.Image != SerialScripter.Properties.Resources.RedOrb)
            ss_RD.Image = SerialScripter.Properties.Resources.RedOrb;
    }
    catch (Exception ex)
    {
        ss_status.Text = ex.Message;
    }
}

********* Exception Text **************
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.Collections.ArrayList.get_Item(Int32 index)
   at System.Windows.Forms.ToolStripItemCollection.get_Item(Int32 index)
   at System.Windows.Forms.ToolStrip.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.StatusStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
4

0 回答 0