我有两节课MainWindow
,Foo
在考虑时间安排方面有一点问题:
class MainWindow : Window
{
internal void SomeMethod(string name)
{
Foo foo = new foo(name)
foo.MyEventHandler += EventHandlerMethod;
}
internal void EventHandlerMethod(object sender, EventArgs e)
{
//do something after foo is done initializing stuff
}
}
class Foo
{
internal event EventHandler MyEventHandler;
internal Foo(string name)
{
//start another thread that will at some point via event call FooMethod()
}
private void FooMethod()
{
MyEventHandler(this, null);
}
}
问题是我无法保证Foo
-initialized 线程将花费多长时间并且FooMethod();
可能在MyEventHandler
添加之前被调用。
我想到了一种可能的解决方案,即不添加初始化程序,而是使用单独的方法,并在添加事件后简单地调用该方法,但一般来说,有没有办法在调用初始化程序之前添加事件?