Cinchoo – Application Host, Part 1

ChoApplicationHost

Building single instance application is easy with Cinchoo framework. Create your application and run using ChoApplicationHost as below. Turning your application to Single Instance application is easy through configuration. Here is how you  can do it. Cinchoo framework uses Mutex to perform the singleton application check by default. It can be overridden to perform custom check.

1. Add reference to Cinchoo.Core.dll

2. Add namespace Cinchoo.Core

[ChoApplicationHost] //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(args);
    }
}

Open the Global Application Settings section in ChoCoreFrx.config file (you can find it in application binary directory), set singleInstanceApp 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" />
    <logSettings traceLevel="4" useApplicationDataFolderAsLogFolder="false">
      <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(new AppHost(), args);
    }
    static void ChoApplication_ApplyFrxParamsOverrides(object sender, ChoFrxParamsEventArgs e)
    {
        e.GlobalApplicationSettings.ApplicationBehaviourSettings.SingleInstanceApp = true;
    }
}

Method 2:

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

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

    protected override void ApplyFrxParamsOverrides(ChoGlobalApplicationSettings globalApplicationSettings, ChoMetaDataFilePathSettings metaDataFilePathSettings)
    {
        globalApplicationSettings.ApplicationBehaviourSettings.SingleInstanceApp = true;
    }
}
public class Program
{
    static void Main(string[] args)
    {
        ChoApplication.Run(args);
    }
}

Custom Singleton Instance Check

By default, Cinchoo framework carries out the Singleton instance check using Mutex. In case, if you want to perform your own custom singleton check for your application, it can be done through by hooking your custom routine to ChoApplication type as below

public class Program
{
    static void Main(string[] args)
    {
        ChoApplication.VerifyAnotherInstanceRunning = (() =>
            {
                //TODO: Do your custom singleton instance check here

                return true; //true, already another instance running. Otherwise false.
            });

        ChoApplication.Run(args);
    }
}
Advertisements

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