Monday, September 21, 2009

How to zero your application startup time

For this question don’t we all of us spend a lot of time ? But I think it is allowed to use a trick to spend our selves that time.

Add to your model to attributes NotifyIcon, MinimizeOnClose. Since I am using Xaf I am going to present that with it but the code should be very similar to any other windows application

so here are our attributes

image

NotifyIcon: will add a tray icon for your app at the system tray with a context menu that will allow you to terminate your application and display it when you double click the icon

image

public partial class NotifyIconController : WindowController

{

    public const string NotifyIconAttributeName = "NotifyIcon";

    public NotifyIconController()

    {

        InitializeComponent();

        RegisterActions(components);

    }

    protected override void OnFrameAssigned()

    {

        base.OnFrameAssigned();

        Frame.TemplateChanged+=FrameOnTemplateChanged;

 

    }

 

    private void FrameOnTemplateChanged(object sender, EventArgs args){

        if (Frame.Context == TemplateContext.ApplicationWindow && Application.Info.GetChildNode("Options").GetAttributeBoolValue(NotifyIconAttributeName))

        {

            var form = Frame.Template as XtraForm;

            if (form != null)

            {

                IContainer  container=new Container();

 

                var strip = new ContextMenuStrip(container);

                strip.Items.Add(GetMenuItem("Maximize",(o,eventArgs) => changeFormVisibility(form)));

                strip.Items.Add(GetMenuItem("Minimize",(o,eventArgs) => changeFormVisibility(form)));

                if (Application is ILogOut)

                    strip.Items.Add(GetMenuItem("LogOut", (o, eventArgs) => ((ILogOut) Application).Logout()));

                strip.Items.Add(GetMenuItem("Exit", (o, eventArgs) => Application.Exit()));

 

                var notifyIcon1 = new NotifyIcon(container){Visible = true, ContextMenuStrip = strip};

                setIcon(notifyIcon1);

                notifyIcon1.DoubleClick += (o, eventArgs) => changeFormVisibility(form);

            }

        }

    }

 

    private ToolStripMenuItem GetMenuItem(string text, EventHandler clickHandler){

        var item = new ToolStripMenuItem(text);

        item.Click+=clickHandler;

        return item;

    }

 

 

    private void changeFormVisibility(XtraForm form){

        if (form.Visible)

            form.Hide();

        else

            form.Show();

    }

 

    private void setIcon(NotifyIcon notifyIcon1){

        string path = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath),"ExpressApp.ico");

        if (File.Exists(path))

            notifyIcon1.Icon=new Icon(path);

        else{

            Stream resourceStream = typeof(eXpandSystemModule).Assembly.GetManifestResourceStream("eXpand.ExpressApp.Resources.ExpressApp.ico");

            if (resourceStream != null) notifyIcon1.Icon = new Icon(resourceStream);

        }

    }

 

 

    public override Schema GetSchema()

    {

        const string CommonTypeInfos = @"<Element Name=""Application"">

                                            <Element Name=""Options"">

                                                <Attribute Name=""" +NotifyIconAttributeName+ @""" Choice=""False,True""/>

                                            </Element>

                                        </Element>";

        return new Schema(new DictionaryXmlReader().ReadFromString(CommonTypeInfos));

    }

 

}

MinimizeOnClose: attribute will prevent your application from closing

public partial class MinimizeOnCloseController : WindowController

{

    private static bool editing;

    public const string MinimizeOnCloseAttributeName = "MinimizeOnClose";

    public MinimizeOnCloseController()

    {

        InitializeComponent();

        RegisterActions(components);

    }

    protected override void OnFrameAssigned()

    {

        base.OnFrameAssigned();

        Frame.TemplateChanged += FrameOnTemplateChanged;

    }

 

    private void FrameOnTemplateChanged(object sender, EventArgs args)

    {

        if (Frame.Context == TemplateContext.ApplicationWindow &&

            Application.Info.GetChildNode("Options").GetAttributeBoolValue(MinimizeOnCloseAttributeName)){

            var form = Frame.Template as XtraForm;

            if (form != null){

                form.FormClosing += FormOnFormClosing;

                SimpleAction action =

                Frame.GetController<DevExpress.ExpressApp.Win.SystemModule.EditModelController>().Action;

                action.Executing+=(o,eventArgs) =>  editing = true;

                action.ExecuteCompleted += (o, eventArgs) => editing = false;

            }

        }

    }

 

    private void FormOnFormClosing(object sender, FormClosingEventArgs e){

        if (!editing && e.CloseReason == CloseReason.UserClosing)

        {

            if (Application != null) e.Cancel = Application.Model.RootNode.GetAttributeBoolValue(MinimizeOnCloseAttributeName, true);

            if (e.Cancel)

                ((XtraForm)sender).Hide();

        }

    }

 

 

    public override Schema GetSchema()

    {

        const string CommonTypeInfos = @"<Element Name=""Application"">

                                            <Element Name=""Options"">

                                                <Attribute Name=""" + MinimizeOnCloseAttributeName + @""" Choice=""False,True""/>

                                            </Element>

                                        </Element>";

        return new Schema(new DictionaryXmlReader().ReadFromString(CommonTypeInfos));

    }

 

}

Now your application never closes and user can display its UI instantaneously but just double clicking the tray icon

Those 2 controllers are already at eXpand and in next version i think i add a LoadWithWindows attribute that will zero my application load time

Technorati Tags: ,,,

Subscribe to XAF feed
Subscribe to community feed

DiggIt!
blog comments powered by Disqus