繁体   English   中英

invalidargument =值'8'对于'索引'无效

[英]invalidargument=value of '8' is not valid for 'index'

因此,我收到错误消息invalidargument = value'8'对于'index'无效,但这应该没问题,因为listview有9(10)个项目。

在此处输入图片说明

在此处输入图片说明

我的代码是这样的:

 private async void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
    {
        passArguments result = (passArguments)e.Argument;
        e.Result = result;

        while (running >= maxRunning)
        {
            editList("Waiting", result.passedFileName, result.passedNum, 1);
            await Task.Delay(500);

        }
        running++;
        editList("Loading...", result.passedFileName, result.passedNum, 0);
        //do stuff
    }

void editList(string message, string fileName, int number, int image)
{
    listView1.BeginUpdate();
    try
    {
        string[] row = { message, fileName };
        var listViewItem = new ListViewItem(row);
        listViewItem.ImageIndex = image;
        listView1.Items[number] = (listViewItem);
    }
    catch (Exception ex)
    {
        MessageBox.Show(number + " | " + ex.Message + Environment.NewLine + fileName + Environment.NewLine + message + Environment.NewLine + "COUNT: "+listView1.Items.Count);
    }

    listView1.EndUpdate();
}

但是,当我删除while循环时,它不会引发错误。 我不确定为什么会这样,有人可以帮助我吗?

编辑:

堆栈跟踪

System.ArgumentException was unhandled by user code
  HResult=-2147024809
  Message=InvalidArgument=Value of '8' is not valid for 'index'.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.RemoveAt(Int32 index)
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.set_Item(Int32 displayIndex, ListViewItem value)
       at System.Windows.Forms.ListView.ListViewItemCollection.set_Item(Int32 index, ListViewItem value)
       at ***.Form1.editList(String message, String fileName, Int32 number, Int32 image) in ***\Form1.cs:line 347
       at ***.Form1.<backgroundWorker3_DoWork>d__c.MoveNext() in ***\Form1.cs:line 369
  InnerException: 

从外观上,您试图将一个项目分配给listview1.Items中超出范围的位置的元素。

如果number不在listview范围内,则listView1.Items[number]将不会扩展项目数。

我会提出这样的建议:

void editList(string message, string fileName, int number, int image)
{
    ListViewItem dummyrow = new ListViewItem(new string[] {"loading", ""});
    listView1.BeginUpdate();
    try
    {
        string[] row = { message, fileName };
        var listViewItem = new ListViewItem(row);
        listViewItem.ImageIndex = image;
        while (listView1.Items.length <= number) {
          //if the listview doesn't have enough rows yet,
          //add a loading message as placeholder
          listView1.Items.add(dummyrow);
        }
        listView1.Items[number] = (listViewItem);
    }
    catch (Exception ex)
    {
        MessageBox.Show(number + " | " + ex.Message + Environment.NewLine + fileName + Environment.NewLine + message + Environment.NewLine + "COUNT: "+listView1.Items.Count);
    }

    listView1.EndUpdate();
}

以下内容不提供答案,但更像是我在寻找答案时发现的一些提示(如果有真实答案,我将删除此帖子)

再次是堆栈跟踪:

  System.ArgumentException was unhandled by user code   HResult=-2147024809
  Message=InvalidArgument=Value of '8' is not valid for 'index'.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.RemoveAt(Int32 index)
       at System.Windows.Forms.ListView.ListViewNativeItemCollection.set_Item(Int32 displayIndex, ListViewItem value)
       at System.Windows.Forms.ListView.ListViewItemCollection.set_Item(Int32 index, ListViewItem value)
       at ***.Form1.editList(String message, String fileName, Int32 number, Int32 image) in ***\Form1.cs:line 347
       at ***.Form1.<backgroundWorker3_DoWork>d__c.MoveNext() in ***\Form1.cs:line 369

以下代码摘自.Net-Framework 4.5.2; 我在代码中添加了自己的注释(但不添加//来表明属于我的注释)

System.ArgumentException抛出在这里:

public virtual void RemoveAt(int index) {
                if (index < 0 || index >= owner.columnHeaders.Length)
                    throw new ArgumentOutOfRangeException("index", SR.GetString(SR.InvalidArgument, "index", (index).ToString(CultureInfo.CurrentCulture)));

                int w = owner.columnHeaders[index].Width; // Update width before detaching from ListView

                // in Tile view our ListView uses the column header collection to update the Tile Information
                if (owner.IsHandleCreated && this.owner.View != View.Tile) {

          !!! important: 
                int retval = unchecked( (int) (long)owner.SendMessage(NativeMethods.LVM_DELETECOLUMN, index, 0));
                                          ^-----^----> quite strange...

        _________________________________________________
        |  Here it is (probably):    
        |            if (0 == retval)
        |                throw new ArgumentException(SR.GetString(SR.InvalidArgument,
        |                                                          "index",
        |                                                          (index).ToString(CultureInfo.CurrentCulture)));
                }

retval的值定义为:

internal IntPtr SendMessage(int msg, int wparam, int lparam) {
            Debug.Assert(IsHandleCreated, "Performance alert!  Calling Control::SendMessage and forcing handle creation.  Re-work control so handle creation is not required to set properties.  If there is no work around, wrap the call in an IsHandleCreated check.");
            return UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), msg, wparam, lparam);
        }

调用方法:

[DllImport(ExternDll.User32, CharSet = CharSet.Auto)]
[ResourceExposure(ResourceScope.None)]
public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, int lParam);

这是来自PInvoke.net对此方法的一些评论

1)使用IntPtr而不是UIntrPtr:UIntPtr类型不符合CLS

2)切勿将“ int”或“ integer”用作lParam。 您的代码将在64位Windows上崩溃。 仅使用IntPtr,“ ref”结构或“ out”结构。

3)永远不要使用“ bool”,“ int”或“ integer”作为返回值。 您的核心将在64位Windows上崩溃。 仅使用IntPtr。 使用bool并不安全-pInvoke无法将IntPtr编组为布尔值。

[...]

2)SendMessage的返回IntPtr可能是IntPtr.Zero


如您所见,调用SendMessage时, lparam0 (->整数)。 (我个人不认为这是导致我们出现问题的原因;但是可以。)


我希望我对那些想深入研究这个问题的人有所帮助。

我想知道为什么 IntPtr可以是IntPtr.Zero可能IntPtr.Zero

研究愉快!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM