2

I have a MainWindow class that launches "baloon" process. It opens a new window within a baloon start to move.

In my MainWindow class, I want to have the number of baloon launched process in real-time, that's why, when I manually close a Baloon Window, I want the MainWindow class to be warned.

Here's what I already wrote :

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");
        }
    }
Rybus
  • 651
  • 6
  • 15
  • 4
    It looks like you're trying to create multiple UI threads there. Don't do that. You're asking for a world of hurt. Just use a single UI thread to manage your entire UI. Only offload non-UI tasks to other threads. – Servy Dec 19 '13 at 16:32

1 Answers1

1

You can create a delegate and pass that to your child instance when you create it.

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");
        }
    }
}

Notice that I pass an instance of RefreshInformationsDelegate to the Ballon constructor. If you want this to happen on window close instead of thread finish then store the callback as an instance variable and call it with callback()in the Form_Closing event.

gb056
  • 149
  • 1
  • 1
  • 7
  • Thanks, once I've stored the callback as an instance variable (`callbackClosing = refreshInformations()` I suppose), where is the `Form_closing` event ? In the Child ? – Rybus Dec 19 '13 at 16:43
  • Try creating a method called as follows `void OnWindowClosing(object sender, CancelEventArgs e){callbackClosing();}` and then adding `Closing += OnWindowClosing;` to your constructor. http://stackoverflow.com/questions/3683450/handling-the-window-closing-event-with-wpf-mvvm-light-toolkit/7225853#7225853 seems to think this will work, I'm not that familiar with WPF – gb056 Dec 19 '13 at 16:51
  • Servy is right in his comment on your question though. I am simply answering **how** to do it, not suggesting you should :) – gb056 Dec 19 '13 at 16:52