3
votes

In Gtk#, I can hook up a handler for the "destroy" event but it never gets called. This application never prints anything out. The Gtk docs say that the "destroy" event is only sent if a certain flag is set, and also says that this flag is automatically set by Gdk.

This is frustrating, since it's basically identical to plain Gtk+ code that works correctly. And how do I know when to call Application.Quit() except by listening to the "destroy" event?

using System;
using Gtk;

public class MainWindow {
    public static void Main(string[] args)
    {
        Application.Init();
        Window win = new Window("Test");
        win.Resize(200, 200);
        win.DestroyEvent += new DestroyEventHandler(OnDestroy);
        win.ShowAll();
        Application.Run();
    }

    private static void OnDestroy(object o, DestroyEventArgs args)
    {
        Console.WriteLine("OnDestroy");
    }
}

P.S. I'm not interested in the "delete" event.

1
Why are you not interested in the delete event? - ptomato
@ptomato: Gtk docs: "delete-event signal is emitted if a user requests that a toplevel window is closed" -- I want to know when the window is closed, not when the user requests it. - Dietrich Epp

1 Answers

4
votes

The gtk-sharp wrapper installs its own destroy event handler that gets invoked first and disconnects your handler. However in turn it provides a Destroyed event. You can use that, as follows:

using System;
using Gtk;

public class MainWindow {
    public static void Main(string[] args)
    {
        Application.Init();
        Window win = new Window("Test");
        win.Resize(200, 200);
        win.Destroyed += new EventHandler(OnDestroy);
        win.ShowAll();
        Application.Run();
    }

    private static void OnDestroy(object o, EventArgs args)
    {
        Console.WriteLine("OnDestroy");
        Application.Quit();
    }
}