注意:これは何よりも好奇心の質問です。
各ウィンドウList<Window>
に、コレクションからウィンドウを削除するCloseイベントに関連付けられたイベントがある場合、コレクションが繰り返されるまで、デリゲート/イベントを使用してCloseイベントの実行を延期するにはどうすればよいでしょうか。
例えば:
public class Foo
{
private List<Window> OpenedWindows { get; set; }
public Foo()
{
OpenedWindows = new List<Window>();
}
public void AddWindow( Window win )
{
win.Closed += OnWindowClosed;
OpenedWindows.Add( win );
}
void OnWindowClosed( object sender, EventArgs e )
{
var win = sender as Window;
if( win != null )
{
OpenedWindows.Remove( win );
}
}
void CloseAllWindows()
{
// obviously will not work because we can't
// remove items as we iterate the collection
// (the close event removes the window from the collection)
OpenedWindows.ForEach( x => x.Close() );
// works fine, but would like to know how to do
// this with delegates / events.
while( OpenedWindows.Any() )
{
OpenedWindows[0].Close();
}
}
}
具体的には、CloseAllWindows()
メソッド内で、コレクションを反復してcloseイベントを呼び出すことができますが、コレクションが完全に反復されるまで、発生するイベントを延期できますか?