title: What's the Correct Way to Run Code After Event Handlers?
body: <p>I am deriving a class from <code>Gtk.Button</code> that is supposed to run some code when the button is clicked, but only <em>after</em> the user-defined event handlers.</p>
  
  <p>Based on .NET conventions, this wouldn't be a problem. The following exemplary Windows Forms code shows a subclass of <code>System.Windows.Forms.Button</code> that executes some code <em>before</em> the event handlers, then invokes the event handlers (by calling the inherited <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onclick.aspx" rel="nofollow"><code>OnClick</code> method</a>; the same would work in WPF with <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.onclick.aspx" rel="nofollow"><code>System.Windows.Controls.Button.OnClick</code></a>), and then executes some code <em>after</em> the event handlers:</p>
  
  <pre><code>using System;
  using System.Windows.Forms;
  using System.Drawing;
  
  namespace ButtonClickedTestSWF
  {
      class Program
      {
          private class MyButton : Button
          {
              protected override void OnClick(EventArgs e)
              {
                  Console.WriteLine("before");
                  base.OnClick(e);
                  Console.WriteLine("after");
              }
          }
  
          [STAThread]
          public static void Main(string[] args)
          {
              using (Form win = new Form() {
                      Text = "Test"
                     }) {
                  win.StartPosition = FormStartPosition.CenterScreen;
                  win.Size = new Size(300, 200);
  
                  MyButton btn = new MyButton();
                  btn.Text = "Button";
                  btn.Click += delegate {
                      Console.WriteLine("event");
                  };
                  btn.Dock = DockStyle.Fill;
                  btn.Parent = win;
  
                  Application.Run(win);
              }
          }
      }
  }
  </code></pre>
  
  <p>As expected, the output is:</p>
  
  <pre><code>before
  event
  after
  </code></pre>
  
  <hr>
  
  <p>However, I have so far failed to replicate this behaviour in Gtk#. Contrary to .NET conventions, the inherited <a href="http://docs.go-mono.com/?link=M%3aGtk.Button.OnClicked%28%29" rel="nofollow"><code>OnClicked</code> method of <code>Gtk.Button</code></a> does not seem to fire the <a href="http://docs.go-mono.com/monodoc.ashx?link=M%3aGtk.Button.Clicked" rel="nofollow"><code>Clicked</code> event</a>. In fact, the docs call the <code>OnClicked</code> method a "default handler".</p>
  
  <p>To verify that Gtk# behaves differently, here's some sample code:</p>
  
  <pre><code>using System;
  
  using Gtk;
  
  namespace ButtonClickedTest
  {
      class Program
      {
          private class MyButton : Button
          {
              public MyButton()
              {
              }
  
              public MyButton(IntPtr raw) : base(raw)
              {
              }
  
              protected override void OnClicked()
              {
                  Console.WriteLine("before");
                  base.OnClicked();
                  Console.WriteLine("after");
              }
          }
  
          [STAThread]
          public static void Main(string[] args)
          {
              Application.Init();
  
              using (Window win = new Window("Test")) {
                  win.WindowPosition = WindowPosition.Center;
                  win.SetSizeRequest(300, 200);
                  win.Hidden += delegate(object sender, EventArgs e) {
                      Application.Quit();
                  };
  
                  MyButton btn = new MyButton();
                  btn.Add(new Label("Button"));
                  btn.Clicked += delegate {
                      Console.WriteLine("event");
                  };
                  win.Add(btn);
  
                  win.ShowAll();
                  Application.Run();
              }
          }
      }
  }
  </code></pre>
  
  <p>Unfortunately, the output is</p>
  
  <pre><code>before
  after
  event
  </code></pre>
  
  <p>i.e. even code at the end of <code>OnClicked</code> runs already before the <code>Clicked</code> event handlers.</p>
  
  <hr>
  
  <p>My question is: <strong>What is the proper way in Gtk# to execute some code upon an event only <em>after</em> the event handlers?</strong></p>
  
  <hr>
  
  <p>I have found two dissatisfying workarounds so far:</p>
  
  <p>The first is to define a replacement for the <code>Clicked</code> event, say, a <code>Clicking</code> event (with an according <code>OnClicking</code> method that fires the <code>Clicking</code> event handlers). I could invoke <code>OnClicking</code> in my overridden version of <code>OnClicked</code> and thus run code before and after the event handlers as I like. Additionally, any subclasses of my button class could use <code>OnClicking</code> in the expected way, i.e. calling <code>base.OnClicking</code> when the <code>Clicking</code> event handlers should run.</p>
  
  <p>This is not very clean, though, because nothing would prevent users of my button class from registering their event handlers with the original <code>Clicked</code> event rather with the new <code>Clicking</code> event that is properly embedded into the button's logic. In particular, I could not even modify the API documentation for the inherited <code>Clicked</code> event to express there that <code>Clicked</code> should not be used and users should turn to <code>Clicking</code> instead.</p>
  
  <p>The other workaround works only in this specific case, as there happens to be an <a href="http://docs.go-mono.com/?link=M%3aGtk.Button.OnReleased%28%29" rel="nofollow"><code>OnReleased</code> method</a> that is always executed after the <code>Clicked</code> event handlers. I could use that method to insert some code that is guaranteed to run only after any <code>Clicked</code> event handlers, but obviously, this solution couldn't be transferred to events that are not conveniently followed by some other events.</p>