///////////////////////////////////////////////////////////////////////////////////////////
// IdleForm.cpp
//
//  Implementation of the IdleForm class, which implements exception handling and
// idle-time processing for Windows Forms.

#include "StdAfx.h"
#include "IdleForm.h"

// Necessary to get hold of System::Windows::Forms::MessageBox
#undef MessageBox

namespace Sunlight
{
    IdleForm::IdleForm()
    {
    }

    // Start the application main loop.
    void IdleForm::Run()
    {
        try
        {
            __raise Init(this, new EventArgs());

            while (Created)
            {
                Application::DoEvents();
                __raise Idle(this, new EventArgs());
            }
        }
        catch(Exception *e)
        {
            // Catch any unhandled exception and kill the device, because we won't see it otherwise...
            Close();

            System::Diagnostics::Debug::WriteLine(String::Format(S"Exception \"{0}\" occurred in {1}.", e->Message, e->Source));
            System::Diagnostics::Debug::WriteLine(String::Format(S"Stack trace: {0}", e->StackTrace));

            MessageBox::Show(e->Message, e->Source, MessageBoxButtons::OK, MessageBoxIcon::Error);
        }
    }
}