如何从另一个线程调用ImageList.Images.Clear()?我试图做一个函数喜欢如何从另一个线程调用ImageList.Images.Clear()?
private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
public static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
{
if (control.InvokeRequired)
{
control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object[] { control, propertyName, propertyValue });
}
else
{
control.GetType().InvokeMember(propertyName, System.Reflection.BindingFlags.SetProperty, null, control, new object[] { propertyValue });
}
}
但ImageList中没有一个InvokeRequired或调用,再加上我不想设置属性,我只是想打电话
ImageList.Images.Clear()
你需要跟踪你的线程上下文。现在看起来可能很痛苦,但是为了让这个方案正确无误,你可以节省自己的时间。 – 2012-02-07 21:26:28
你的代码有一个问题 - 如果控件/它的父类的'Handle'没有被创建,那么'InvokeRequired'将返回'false'。在第一次使用代码之前,您必须确保已在UI线程上创建了“Handle”,否则您的应用程序将遇到奇怪的麻烦! – Yahia 2012-02-07 21:28:41