Handling Application Events: On Thread Exit


   


using System;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
   

public class HelloWorldForm : Form
{
    public HelloWorldForm()
    {
        Text = "Hello, WindowsForms!";
    }
}
   
public class ApplicationEventHandlerClass
{
   
    public void OnThreadExit(object sender, EventArgs e)
    {
        Console.WriteLine("The application's main thread is shutting down.");
    }
}
   
public class MainClass
{
    public static void Main()
    {
        HelloWorldForm FormObject = new HelloWorldForm();
        ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
   
        Application.ThreadExit += new EventHandler(AppEvents.OnThreadExit);
        Application.Run(FormObject);
    }
}
           
          


Handling Application Events: Thread Exception


   

using System;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
   

public class HelloWorldForm : Form
{
    public HelloWorldForm()
    {
        Text = "Hello, WindowsForms!";
    }
}
   
public class ApplicationEventHandlerClass
{
   
    public void OnThreadException(object sender, ThreadExceptionEventArgs e)
    {
        Console.WriteLine("an exception thrown from an application thread was caught!");
    }
}
   
public class MainClass
{
    public static void Main()
    {
        HelloWorldForm FormObject = new HelloWorldForm();
        ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
   
        Application.ThreadException += new ThreadExceptionEventHandler(AppEvents.OnThreadException);
        Application.Run(FormObject);
    }
}
           
          


Handling Application Events: On Idle


   

using System;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
   

public class HelloWorldForm : Form
{
    public HelloWorldForm()
    {
        Text = "Hello, WindowsForms!";
    }
}
   
public class ApplicationEventHandlerClass
{
    public void OnIdle(object sender, EventArgs e)
    {
        Console.WriteLine("The application is idle.");
    }
}
   
public class MainClass
{
    public static void Main()
    {
        HelloWorldForm FormObject = new HelloWorldForm();
        ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
   
        Application.Idle += new EventHandler(AppEvents.OnIdle);
        Application.Run(FormObject);
    }
}

           
          


Handling Application Events: On Application Exit


   

using System;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
   

public class HelloWorldForm : Form
{
    public HelloWorldForm()
    {
        Text = "Hello, WindowsForms!";
    }
}
   
public class ApplicationEventHandlerClass
{
    public void OnApplicationExit(object sender, EventArgs e)
    {
        try
        {
            Console.WriteLine("The application is shutting down.");
        }
        catch(NotSupportedException)
        {
        }
    }
}
   
public class MainClass
{
    public static void Main()
    {
        HelloWorldForm FormObject = new HelloWorldForm();
        ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
   
        Application.ApplicationExit += new EventHandler(AppEvents.OnApplicationExit);
        Application.Run(FormObject);
    }
}

           
          


Handling Application Events: OnIdle, OnThreadException, OnThreadExit

   
 


using System;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;

public class HelloWorldForm : Form {
    public HelloWorldForm() {
    }

    public static void OnApplicationExit(object sender, EventArgs e) {
        try {
            Console.WriteLine("shutting down.");
        } catch (NotSupportedException) {
        }
    }

    public static void OnIdle(object sender, EventArgs e) {
        Console.WriteLine("idle.");
    }

    public static void OnThreadException(object sender, ThreadExceptionEventArgs e) {
        Console.WriteLine("caught!");
    }

    public static void OnThreadExit(object sender, EventArgs e) {
        Console.WriteLine("thread is shutting down.");
    }

    public static void Main() {
        HelloWorldForm FormObject = new HelloWorldForm();

        Application.ApplicationExit += new EventHandler(OnApplicationExit);
        Application.Idle += new EventHandler(OnIdle);
        Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);
        Application.ThreadExit += new EventHandler(OnThreadExit);
        Application.Run(FormObject);
    }
}

    


OnUnhandledException method is added to the AppDomain.UnhandledException event

   
 

using System;

public class Starter {
    public static void Main() {
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
        int vara = 5, varb = 0;
        vara /= varb;
    }

    public static void OnUnhandledException(
            object sender, UnhandledExceptionEventArgs e) {
        string application_name = sender.ToString();
        Exception except = (Exception)e.ExceptionObject;
        string errormessage = "Application " + application_name +
            " [ Exception " + except.Message + " ]";
        Console.WriteLine(errormessage);
    }
}