因此我收到错误消息invalidargument =' 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:
答案 0 :(得分:1)
从外观上看,您尝试将某个项目分配到listview1.Items
中某个位置超出范围的元素。
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();
}
答案 1 :(得分:0)
以下内容未提供答案,但更像是我找到答案的提示集合(当有真实答案时我会删除此帖子)
堆栈跟踪:
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位窗口上崩溃。只使用IntPtr,“ref”结构或“out”结构。
3)永远不要使用“bool”,“int”或“integer”作为返回值。您的核心将在64位窗口上崩溃。只使用IntPtr。使用bool是不安全的 - pInvoke不能将IntPtr编组为布尔值。
[...]
2)SendMessage的返回IntPtr可能是IntPtr.Zero
在调用lparam
时,您可以看到0
是SendMessage
( - &gt;整数)。
(我个人认为这不是我们问题的原因;但它可以。)
我希望我帮助那些想要深入解决这个问题的人。
我想知道为什么 IntPtr
可能是IntPtr.Zero
快乐的研究!