0

別のスレッドから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()
4

1 に答える 1

3

あなたはこれを使うことができます:

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

これにより、UIスレッドでデリゲートが非同期的に呼び出されます。リストをすぐにクリアする必要がある場合は、PostをSendに置き換えてください。もちろん、クリアしたいImageListへの参照も必要です。

于 2012-02-07T21:37:26.933 に答える