Cinchoo – Application Host, Part 2

ChoApplicationHost

Using Cinchoo framework, you can set your application to run at windows start-up easily. Here is how you can do it

1. Add reference to Cinchoo.Core.dll

2. Add namespace Cinchoo.Core

[RunInstaller(true)] //Optional attribute, to host your application as Windows Service
public class AppHost : ChoApplicationHost
{
    protected override void OnStart(string[] args)
    {
        //TODO: Application Startup code goes here
    }
}

public class Program
{
    static void Main(string[] args)
    {
        ChoApplication.Run<AppHost>(args);
    }
}

Open the Global Application Settings section in ChoCoreFrx.xml file (you can find it in application binary directory), set runAtStartup to true.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <globalApplicationSettings applicationId="TestApplication.exe" eventLogSourceName="TestApplication.exe" turnOnConsoleOutput="false">
    <behaviourSettings hideWindow="false" bringWindowToTop="false" alwaysOnTop="false" runAtStartup="false" runOnceAtStartup="false" singleInstanceApp="false" activateFirstInstance="false" />
    <trayApplicationBehaviourSettings turnOn="true" showInTaskbar="true" hideMainWindowAtStartup="true" hideTrayIconWhenMainWindowShown="false" trayAppTurnOnMode="OnMinimize" />
    <logSettings traceLevel="4">
      <logFolder />
      <logFilename>TestApplication.exe</logFilename>
      <logTimeStampFormat>yyyy-MM-dd hh:mm:ss.fffffff</logTimeStampFormat>
    </logSettings>
    <appConfigPath />
  </globalApplicationSettings>
</configuration>

Override programmatically

Some scenarios you may want to control it programmatically. When it arises, you can do so as below

Method 1:

Subscribe to ApplyFrxParamOverrides eventhandler before ChoApplication.Run() as below.

public class Program
{
    static void Main(string[] args)
    {
        ChoApplication.ApplyFrxParamsOverrides += new EventHandler<ChoFrxParamsEventArgs>(ChoApplication_ApplyFrxParamsOverrides);
        ChoApplication.Run<AppHost>(args);
    }
    static void ChoApplication_ApplyFrxParamsOverrides(object sender, ChoFrxParamsEventArgs e)
    {
        e.GlobalApplicationSettings.ApplicationBehaviourSettings.RunAtStartup= true;
    }
}

Method 2:

You can override ApplyFrxParamsOverrides method in your AppHost class as below.

[RunInstaller(true)]
public class AppHost : ChoApplicationHost
{
    protected override void OnStart(string[] args)
    {
    }

    protected override void ApplyFrxParamsOverrides(ChoGlobalApplicationSettings globalApplicationSettings, ChoMetaDataFilePathSettings metaDataFilePathSettings)
    {
        globalApplicationSettings.ApplicationBehaviourSettings.RunAtStartup = true;
    }
}
public class Program
{
    static void Main(string[] args)
    {
        ChoApplication.Run<AppHost>(args);
    }
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: