我有一个MainWindow
课程,可以启动“气球”流程。它打开了一个气球开始移动的新窗口。
在我的MainWindow课程中,我希望实时获得baloon启动过程的数量,这就是为什么当我手动关闭Baloon Window时,我希望警告MainWindow类。
以下是我已写的内容:
private void LaunchBaloonProces(object sender, RoutedEventArgs e)
{
if (countBaloonProcess() < 5) {
Application app = System.Windows.Application.Current;
ClassLibrary1.Ballon b = new Ballon();
Thread unThread = new Thread(new ThreadStart(b.Go));
unThread.Start();
if (allPaused) unThread.Suspend();
unThread.Name = "Ballon";
processList.AddLast(unThread);
refreshInformations(); // <= Here's the method Thread child should call
}
else {
informations.SetValue(TextBox.TextProperty, "Can't create more than 5 baloons");
}
}
答案 0 :(得分:0)
您可以创建委托并在创建委托时将其传递给子实例。
E.g:
class ChildWindow{
public ChildWindow(MainWindow.RefreshInformationsDelegate callback){
//Do work
//Notify MainWindow that we are closing
callback();
}
}
class MainWindow{
public static delegate void RefreshInformationsDelegate();
private void LaunchBaloonProces(object sender, RoutedEventArgs e)
{
if (countBaloonProcess() < 5) {
Application app = System.Windows.Application.Current;
ClassLibrary1.Ballon b = new Ballon(new RefreshInformationsDelegate(refreshInformations));
Thread unThread = new Thread(new ThreadStart(b.Go));
unThread.Start();
if (allPaused) unThread.Suspend();
unThread.Name = "Ballon";
processList.AddLast(unThread);
}
else {
informations.SetValue(TextBox.TextProperty, "Can't create more than 5 baloons");
}
}
}
请注意,我将RefreshInformationsDelegate
的实例传递给Ballon
构造函数。如果您希望在窗口关闭而不是线程完成时发生这种情况,则将回调存储为实例变量,并在callback()
事件中使用Form_Closing
进行调用。