Invoke
requires you to use a delegate as first parameter. ListBox.Items.Add
is a method, not a delegate.
You can, however, use this:
this.Invoke((Action)delegate()
{
listBox1.Items.Add(e.Data.ToString());
});
题
I don't know how to explain that, so here is my code:
this.Invoke(listBox1.Items.Add, new Object[] { e.Data.ToString() });
But, everything would be ok, if I wouldn't have any errors, but sadly I have 2 errors:
Error 1
The best overloaded method match for 'System.Windows.Forms.Control.Invoke(System.Delegate, params object[])' has some invalid arguments
Error 2
Argument 1: cannot convert from 'method group' to 'System.Delegate'
What should I do to overcome these exceptions?
解决方案
Invoke
requires you to use a delegate as first parameter. ListBox.Items.Add
is a method, not a delegate.
You can, however, use this:
this.Invoke((Action)delegate()
{
listBox1.Items.Add(e.Data.ToString());
});
其他提示
Set a delegate to work with.
Invoke(new Action(() =>
{
// Do it
}));
You batter check if te control needs invoke, if so invoke it:
if(listBox1.InvokeRequired)
listBox1.Invoke((Action)(()=> listBox1.Items.Add(e.Data.ToString())); //if e.Data is the object to add it to listBox!