C# invalidargument=的值';8';对';无效;指数';
因此,我收到的错误消息invalidargument=值“8”对“index”无效 但这应该没问题,因为listview有9(10)个项目 我的代码是:C# invalidargument=的值';8';对';无效;指数';,c#,C#,因此,我收到的错误消息invalidargument=值“8”对“index”无效 但这应该没问题,因为listview有9(10)个项目 我的代码是: private async void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e) { passArguments result = (passArguments)e.Argument; e.Result = result;
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:
用户代码未处理System.ArgumentException
HResult=-2147024809
Message=InvalidArgument=值“8”对“index”无效。
Source=System.Windows.Forms
堆栈跟踪:
位于System.Windows.Forms.ListView.ListViewNativeItemCollection.RemoveAt(Int32索引)
位于System.Windows.Forms.ListView.ListViewNativeItemCollection.set_项(Int32 displayIndex,ListViewItem值)
位于System.Windows.Forms.ListView.ListViewItemCollection.set_项(Int32索引,ListViewItem值)
在***\Form1.cs中的***.Form1.editList(字符串消息、字符串文件名、Int32编号、Int32图像):第347行
在***\Form1.cs中的***.Form1.d_uc.MoveNext()处:第369行
内部异常:
从外观上看,您试图将项目分配给
列表视图1中的元素。项目位于超出范围的位置
listview 1.如果项目编号不在listview的范围内,Items[编号]
将不会扩展项目编号
我建议这样做:
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();
}
void editList(字符串消息、字符串文件名、整数编号、整数图像)
{
ListViewItem dummyrow=新的ListViewItem(新字符串[]{“正在加载”,“”});
listView1.BeginUpdate();
尝试
{
字符串[]行={消息,文件名};
var listViewItem=新listViewItem(行);
listViewItem.ImageIndex=图像;
虽然(listView1.Items.length以下内容没有提供答案,但更像是我在搜索答案时发现的一系列提示(如果有真实答案,我将删除此帖子)
再次显示堆栈跟踪:
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
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”用作LPRAM。您的代码将在64位windows上崩溃。仅使用IntPtr、“ref”结构或“out”结构
3) 切勿使用“bool”、“int”或“integer”作为返回值。您的内核将在64位windows上崩溃。仅使用IntPtr。使用bool不安全-pInvoke无法将IntPtr封送为布尔值
[……]
2) SendMessage的返回IntPtr可能是IntPtr.Zero
如您所见,调用sendmages
时,lparam
为0
(->整数)。
(我个人不认为这是我们问题的原因;但它可以。)
我希望我已经帮助了那些想深入研究这个问题的人
我想知道为什么IntPtr
可能是IntPtr.Zero
快乐搜索!也许listView1
包含的项目少于9项…editList在另一个(非UI线程)中被调用。使用BeginInvoke()调用editList。@DavidG我刚刚添加了另一个图像,显示它有多少项不是“正在运行的++”放错位置?应该在循环内?不,只有当运行大于“x”时,它才会进入循环,一旦它降到“x”之下,它就会退出循环并向运行添加1。你能生成完整的堆栈跟踪而不仅仅是消息吗?到底是什么引发了异常?我刚刚添加了它,看一看
[DllImport(ExternDll.User32, CharSet = CharSet.Auto)]
[ResourceExposure(ResourceScope.None)]
public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, int lParam);