#region License
/**
* Ingenious MVC : An MVC framework for .NET 2.0
* Copyright (C) 2006, JDP Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: - Kent Boogaart (kentcb@internode.on.net)
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Ingenious.Mvc.Configuration.Configurators;
using Ingenious.Mvc.Util;
namespace Ingenious.Mvc.Windows.Forms{
/// <include file='ApplicationContext.doc.xml' path='/doc/member[@name="T:ApplicationContext"]/*'/>
public class ApplicationContext : System.Windows.Forms.ApplicationContext
{
private Task _mainTask;
private static readonly Log _log = Log.CreateForType(typeof(ApplicationContext));
/// <include file='ApplicationContext.doc.xml' path='/doc/member[@name="P:MainTask"]/*'/>
public Task MainTask
{
get
{
return _mainTask;
}
}
/// <include file='ApplicationContext.doc.xml' path='/doc/member[@name="M:.ctor(Ingenious.Mvc.Id`1)"]/*'/>
public ApplicationContext(Id<INavigator> startingNavigator) : this(startingNavigator, null)
{
}
/// <include file='ApplicationContext.doc.xml' path='/doc/member[@name="M:.ctor(Ingenious.Mvc.Id`1,System.Object)"]/*'/>
public ApplicationContext(Id<INavigator> startingNavigator, object taskData)
{
ArgumentHelper.AssertNotEmpty(startingNavigator, "startingNavigator");
_log.Verbose("Constructing ApplicationContext with starting navigator ID '{0}'.", startingNavigator);
//configure the Ingenious MVC framework
TaskManager taskManager = GetTaskManager();
//note: the design of the Windows Forms ApplicationContext class forces us to put a lot of logic in our constructor instead of a
//separate method. That means we have to do nasty things like invoke virtual methods from our constructor. Yuk.
Configure(taskManager);
//listen for ending tasks so we know when to shut down the app
taskManager.TaskEnded += new EventHandler<TaskEventArgs>(taskManager_TaskEnded);
//now start the specified navigator
_mainTask = taskManager.StartTask(startingNavigator, taskData);
}
/// <include file='ApplicationContext.doc.xml' path='/doc/member[@name="M:GetTaskManager()"]/*'/>
protected virtual TaskManager GetTaskManager()
{
//use the default TaskManager instance unless this method is overridden
return TaskManager.Instance;
}
/// <include file='ApplicationContext.doc.xml' path='/doc/member[@name="M:Configure(Ingenious.Mvc.TaskManager)"]/*'/>
protected virtual void Configure(TaskManager taskManager)
{
//use the config section configurator unless this method is overridden
ConfigSectionConfigurator.Configure(taskManager);
}
private void taskManager_TaskEnded(object sender, TaskEventArgs e)
{
TaskManager taskManager = (TaskManager) sender;
if (taskManager.Tasks.Count == 0)
{
//no tasks left running so shut down the app
_log.Verbose("No tasks still executing - shutting down application.");
ExitThread();
}
else
{
_log.Verbose("There are still {0} tasks running - application will not shut down yet.", taskManager.Tasks.Count);
}
}
}
}
|