Threads: BeginInvoke howto add a complete ListviewItem?
how can i add listview subitems through the BeginInvoke -> myDelegate process ? I've tried various versions of solutions, but allways with error!
thanks a lot for help!
public delegate ListViewItem myDelegate(string aItem);
...
if (InvokeRequired)
{
ListViewItem lvwItem = new ListViewItem();
lvwItem.Text = strMessage;
lvwItem.SubItems.Add(".2.");
BeginInvoke(new myDelegate(listView1.Items.Add), new string[] { strMessage }); //works with a var of type string, but not for more Subitems.add()
}
i found the answer, but i can't post it as answer because i have to wait up 7 hours....
i found the solution without crashing my multithreaded app... i'm stupid ;)
in my delegate i had used String instead of Listviewitem!
public delegate ListViewItem myDelegate2(ListViewItem aItem);
...
if (InvokeRequired)
{
//BeginInvoke(new myDelegate(listView1.Items.Add), new object[] { strMessage });
ListViewItem lvwItem 开发者_JAVA百科= new ListViewItem();
lvwItem.Text = strMessage;
lvwItem.SubItems.Add(".2.");
BeginInvoke(new myDelegate2(listView1.Items.Add), new object[] { lvwItem });
}
You do seem to be getting rather tied up. Try this:
ListViewItem lvwItem = new ListViewItem();
lvwItem.Text = strMessage;
lvwItem.SubItems.Add(".2.");
if (listView1.InvokeRequired)
listView1.BeginInvoke(new MethodInvoker(() => listView1.Items.Add(lvwItem)));
else
listView1.Items.Add(lvwItem);
精彩评论