0
我想在类文件方法中使用deletegate从异步调用的checklistbox(winform控件)中移除checked项。但它向我显示了以下错误消息: -跨线程操作无效:从其创建线程以外的其他线程访问
跨线程操作无效:从其创建的线程以外的线程访问控件'checkedListBox1'。
我已经尝试过调用所需,但又得到了同样的错误。示例代码如下:
private void button1_Click(object sender, EventArgs e)
{
// Create an instance of the test class.
Class1 ad = new Class1();
// Create the delegate.
AsyncMethodCaller1 caller = new AsyncMethodCaller1(ad.TestMethod1);
//callback delegate
IAsyncResult result = caller.BeginInvoke(checkedListBox1,
new AsyncCallback(CallbackMethod)," ");
}
在类文件代码TestMethod1是: -
private delegate void dlgInvoke(CheckedListBox c, Int32 str);
private void Invoke(CheckedListBox c, Int32 str)
{
if (c.InvokeRequired)
{
c.Invoke(new dlgInvoke(Invoke), c, str);
c.Items.RemoveAt(str);
}
else
{
c.Text = "";
}
}
// The method to be executed asynchronously.
public string TestMethod1(CheckedListBox chklist)
{
for (int i = 0; i < 10; i++)
{
string chkValue = chklist.CheckedItems[i].ToString();
//do some other database operation based on checked items.
Int32 index = chklist.FindString(chkValue);
Invoke(chklist, index);
}
return "";
}
是的,我确信这一点。 – user307524 2010-04-03 09:56:03