/*
* Copyright (c) 2009 Craig Sutherland
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
namespace FastForward.Monitor
{
using System;
using System.Windows.Forms;
using FastForward.WinCore;
///
/// The application context for the monitor.
///
public class MonitorApplicationContext
: ApplicationContext, IDisposable
{
#region Private fields
private ApplicationConfig configuration;
private ServerList servers;
private MainForm mainForm;
private bool isInitialised;
private Timer timer = new Timer();
#endregion
#region Constructors
///
/// Initialise a new .
///
public MonitorApplicationContext()
{
AppDomain.CurrentDomain.UnhandledException += (o, e) =>
{
if (e.ExceptionObject is Exception)
{
var errorLog = (e.ExceptionObject as Exception).SaveLog();
MessageBox.Show("An unhandled error occurred in the application, the application state is now unstable. It is recommended you close the application now." + Environment.NewLine + "The error log was written to " + errorLog, "Unhandled error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
else
{
MessageBox.Show("An unhandled error occurred in the application, the application state is now unstable. It is recommended you close the application now.", "Unhandled error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
};
MainForm = new SplashForm();
MainForm.Show();
Application.DoEvents(); // Make sure the splash form is loaded
this.timer.Interval = 250;
this.timer.Tick += this.LoadApplication;
this.timer.Start();
}
#endregion
#region Public methods
#region Dispose()
///
/// Clean up.
///
///
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (servers != null)
{
servers.Dispose();
servers = null;
}
}
#endregion
#endregion
#region Protected methods
#region OnMainFormClosed()
///
/// Switches from the splash form to the main form.
///
///
///
protected override void OnMainFormClosed(object sender, EventArgs e)
{
if (isInitialised && (MainForm is SplashForm))
{
MainForm = mainForm;
MainForm.Show();
}
else
{
if (isInitialised)
{
configuration.Save();
}
this.ExitThread();
}
}
#endregion
#endregion
#region Private methods
#region LoadApplication()
///
/// Performs the load of the application configuration.
///
private void LoadApplication(object sender, EventArgs args)
{
this.timer.Stop();
try
{
using (Trace.Start("Loading application"))
{
// Initialise the servers
configuration = ApplicationConfig.Load();
servers = new ServerList();
foreach (var server in configuration.Servers)
{
using (Trace.Start("Adding server: " + server.Address))
{
servers.AddServer(server);
}
}
// Load an initialise the main form
mainForm = new MainForm(servers, configuration);
mainForm.InitialiseForm();
// Close the splash form
isInitialised = true;
MainForm.Close();
}
}
catch (Exception error)
{
using (Trace.Start("Handling load error"))
{
// Make sure the servers are shutdown otherwise we get repeating errors!
if (servers != null)
{
servers.Dispose();
servers = null;
}
// Shut down the main form if loaded
if (mainForm != null)
{
mainForm.Terminate();
}
// Shut down after any start-up error
var errorLog = error.SaveLog();
MessageBox.Show("An unhandled error occurred while loading the application, therefore the application will close." + Environment.NewLine + "The error log was written to " + errorLog, "Unhandled error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
MainForm.Close();
}
}
}
#endregion
#endregion
}
}