如何从另一个线程调用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或Invoke,而且我不想设置属性,我只想调用

ImageList.Images.Clear()

您可以使用这个:

System.Threading.SynchronizationContext.Current.Post(o => ImageList.Images.Clear(), null);

这将异步调用UI线程上的委托。如果需要立即清除列表,请将"Post"替换为"Send"。当然,您还需要对要清除的ImageList的引用。

最新更新