Tag Archives: Auto

Cinchoo – Turn your app to Windows Tray app

In this article, I’ll show you how to turn your console / winform application to Windows System Tray application. Cinchoo framework provides a single hosting infrastructure to turn your application either Windows Service or Windows Tray application through configuration.

Console Application

Here is how you can do it for console application

1. Create a new ‘Console Application‘ from VS.NET

2. Add reference to Cinchoo.Core.dll

3. Add namespace Cinchoo.Core

4. Create a class derived from ChoApplicationHost as below

[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
    protected override void OnStart(string[] args)
    {
        //TODO: Application Startup code goes here
        base.OnStart(args);
    }
}

Decorating the above class with ChoApplicationHostAttribute will make the application to be run as Windows Service. And override OnStart method, where application start up code placed there.

5. In the main entry, do as below.

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

That’s all, you application is now ready to run as self installable windows service application or Windows Tray application.

Here is how to turn your application to Windows Tray application. In ChoCoreFrx.xml file, set ‘turnOn’ flag to ‘true’ in trayApplicationBehaviourSettings element.

<?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" />
    <appConfigPath />
  </globalApplicationSettings>
</configuration>

Other parameters

  • showInTaskbar – true, will show the application in Taskbar. false, otherwise.
  • hideMainWindowAtStartup – true, will hide the window at the application startup. Otherwise false.
  • hideTrayIconWhenMainWindowShown – true, tray application icon will hidden when the main windows shown. Otherwise false.
  • trayAppTurnOnMode – This option is applicable to WinForm application only. Possible options are OnMinimize, OnClose, OnMinimizeOrClose.

WinForm Application

Below are the steps to turn your winform application into Windows Tray application

1. Create a new ‘WinForm Application‘ from VS.NET

2. Add reference to Cinchoo.Core.dll

3. Add namespace Cinchoo.Core

4. Create a class derived from ChoApplicationHost and IChoWinFormApp as below

[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
    MainForm form = new MainForm();
    public AppHost()
    {
    }
    public Form MainFormWindow
    {
        get { return form; }
    }

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);
    }
}

Decorating the above class with ChoApplicationHostAttribute will make the application to be run as Windows Service. And override OnStart method, where application start up code placed there.

5. In the main entry, do as below.

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

Thats all. Try it.

Advertisement

Cinchoo – Windows Service made easy

Cinchoo framework simplifies the Windows service development. An application developed in this approach is ready to run as Console application as well as self install-able Service application. In this article, I’m going to walk over the steps of creating service development.

1. Create a new ‘Console Application‘ from VS.NET

2. Add reference to Cinchoo.Core.dll

3. Add namespace Cinchoo.Core

4. Create a class derived from ChoApplicationHost as below

[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
    protected override void OnStart(string[] args)
    {
        //TODO: Application Startup code goes here
    }
}

Make sure the type is decorated with ChoApplicationHostAttribute. And override OnStart method, where application start up code placed there. Implement any other service related (OnStop, OnShutdown etc) methods by overriding them.

5. In the main entry, do as below.

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

That’s all, you application is self install able windows service application.

You can run it as console application or install it as Windows service also.

To Install as windows service, pass /@i command line argument

[AppExeName].exe /@i

To uninstall as windows service, pass /@u command line argument

[AppExeName].exe /@u

To start the service, pass /@s command line argument

[AppExeName].exe /@s

To stop the service, pass /@t command line argument

[AppExeName].exe /@t

To pause the service, pass /@p command line argument

[AppExeName].exe /@p

To continue the service, pass /@c command line argument

[AppExeName].exe /@c

To execute a command in the service, pass /@e:{command_id} command line argument

[AppExeName].exe /@e:2

To get the help on Windows service commands, pass /@?

C:\>TestServiceApp.exe /@?
TestServiceApp [Version 1.0.0.0]
Copyright c  2014

TESTSERVICEAPP [/@SN:<string>] [/@SD:<string>] [/@I] [/@U] [/@S] [/@T] [/@P]
[/@C] [/@E:<int>] [/@SP:<string>]

        /@SN    Service Name.
        /@SD    Service Description.
        /@I     Install Service.
        /@U     Uninstall Service.
        /@S     Start Service.
        /@T     Stop Service.
        /@P     Pause Service.
        /@C     Continue Service.
        /@E     Execute Command.
        /@SP    Command Line Parameters.

Controlling Service Installation

There are couple of settings classes which controls the service process behavior during installation. It can configured through configuration file or can be overridden programmatically. They are

  • ChoServiceInstallerSettings – service process parameters used during installation of service.
  • ChoServiceProcessInstallerSettings – service credential parameters used during installation of the service.

First, let take a look at the way configuring them through configuration file.

ChoServiceProcessInstallerSettings

These setting can be maintained in ChoServiceProcessInstallerSettings.xml file. If the file not exists, it will created by Cinchoo framework in application configuration directory. The file look as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <serviceProcessInstallerSettings account="LocalSystem" userName="" password="">
    <helpText />
  </serviceProcessInstallerSettings>
</configuration>

If you want more information about each attribute/element in the above xml section, please visit ServiceProcessInstaller Class in MSDN.

ChoServiceInstallerSettings

These setting can be maintained in ChoServiceInstallerSettings.xml file. If the file not exists, it will created by Cinchoo framework in application configuration directory. The file look as below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <serviceInstallerSettings delayedAutoStart="false" displayName="ChoServiceHost.Test" serviceName="ChoServiceHost.Test" serviceStartMode="Automatic" />
</configuration>

If you want more information about each attribute/element in the above xml section, please visit ServiceInstaller Class in MSDN.

Next, let take a look at the way of overriding them programmatically.

Override ApplyServiceInstallParamsOverrides method in your ApplicationHost class, where you can changes service installation parameters.

[ChoApplicationHost]
public class AppHost : ChoApplicationHost
{
    protected override void OnStart(string[] args)
    {
        Console.WriteLine("Application started...");
    }

    protected override void ApplyServiceInstallParamsOverrides(ChoServiceProcessInstallerSettings serviceProcessInstallerSettings, ChoServiceInstallerSettings serviceInstallerSettings)
    {
        serviceInstallerSettings.DisplayName = "Test";
        serviceInstallerSettings.ServiceName = "Test";
    }
}

In the above sample code, we are trying to override the ServiceName and DisplayName as ‘Test’. When you install the service, it will be created as ‘Test’ service.

Cinchoo – Framework Tips

Here I’m going to talk about couple of tips on using Cinchoo framework in your project.

At the start of the application, you must call ChoFramework.Initialize() to initialize the framework and start necessary services. Usually you must place this statement in application Main entry point as below.

Calling ChoFramework.Shutdown() method is not mandatory to call. But it is safe to make this call for graceful shutdown of all the background services.

static void Main(string[] args)
{
    ChoFramework.Initialize();
    try
    {
        ChoApplication.ApplyFrxParamsOverrides += new EventHandler<ChoFrxParamsEventArgs>(ChoApplication_ApplyFrxParamsOverrides);

        SampleConfigSection ApplicationSettings = new SampleConfigSection();
    }
    catch (Exception ex)
    {
        Console.WriteLine("ERROR: " + ex.Message);
    }
    finally
    {
        ChoFramework.Shutdown();
    }
}

ChoAppDomain.

Cinchoo – Overriding Framework Configuration Parameters

In this section, I’ll go over the list of Cinchoo framework configuration parameters can be overridden to control the run-time environment of the framework either through configuration or programmatically.

Framework will run fine without overriding these parameters. Only when you need, you can override them. In those situation, it can be done through configuration or programmatically.

First, we will see how to override them through configuration. All the framework parameters must be stored in ChoCoreFrx.config file under application executable folder. This is the default behavior of discovering this file by the framework. But this can be overridden by specifying the path to this file through appFrxSettings custom configuration section in App.config file as below

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="appFrxSettings" type="Cinchoo.Core.ChoAppFrxSettings, Cinchoo.Core" />
    </configSections>
    <appFrxSettings appFrxFilePath="C:\ConfigFolder" />
</configuration>

GlobalApplicationSettings

There are the application level settings used by framework. Below are the attributes and their usages.

  • globalApplicationSettings – Global application parameters used by framework. All the variables are optional.
    • singleInstanceApp [bool] – true, to ensure there is only one instance of the application running at any point in time. false, lets you to run multiple instances of the application. Default is false.
    • applicationId [string] – the custom application name. Default value is the executable name.
    • eventLogSourceName [string] – Event log source name where all the errors are posted on this name, default value is applicationId.
    • useApplicationDataFolderAsLogFolder [bool] – true, it uses the current roaming user directory. Default is false.
    • logFolder [string] – A log directory, where all the log files are created by framework. Default is ‘[appExePath]\Logs’.
    • appConfigPath [string] – configuration file path. If not specified, this will be [appExeFolder].[appExeName].xml
    • logTimeStampFormat [string] – A time stamp format used when logging information to log files. Default value is “yyyy-MM-dd hh:mm:ss.fffffff”
    • traceLevel [int] – Specifies what message to output for the Trace/ChoTrace classes. 0-Off, 1-Error, 2-Warning, 3-Info, 4-Verbose.

Here is the sample Global Application Settings section in ChoCoreFrx.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <globalApplicationSettings singleInstanceApp="false" applicationId="TestApplication" eventLogSourceName="TestApplication" useApplicationDataFolderAsLogFolder="false">
    <logFolder>C:\Logs</logFolder>
    <appConfigPath>c:\config\TestApplication.xml</appConfigPath>
    <logTimeStampFormat>yyyy-MM-dd hh:mm:ss.fffffff</logTimeStampFormat>
  </globalApplicationSettings>
</configuration>

MetaDataFilePathSettings

Cinchoo framework creates as well as uses number of Meta-Data files, these file paths can be configured through ChoCoreFrx.config file.

  • configurationMetaDataFilePath – meta data file path used by Configuration manager. By default, it’s value is [appExeName].config.meta
  • pcMetaDataFilePath – meta data file path used by Performance Counter manager. By default, its value is [appExeName].perf.meta

Here is the sample xml section looks like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <metaDataFilePathSettings>
    <configurationMetaDataFilePath>c:\Config\configurationMetaFile.meta</configurationMetaDataFilePath>
    <pcMetaDataFilePath>c:\Config\PCMetaFile.meta</pcMetaDataFilePath>
  </metaDataFilePathSettings>
</configuration>

Run-Time approach

Lets see how we can override the system parameters at run-time in here. Cinchoo framework exposes ChoApplication.ApplyFrxParamsOverrides event. By subscribing to this event, you have a chance to override the framework parameters at run-time. See below the sample code on how to override some of the system parameters

static void Main(string[] args)
{
    try
    {
        ChoApplication.ApplyFrxParamsOverrides += new EventHandler<ChoFrxParamsEventArgs>(ChoApplication_ApplyFrxParamsOverrides);
        XmlApplicationSettings ApplicationSettings = new XmlApplicationSettings();
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

static void ChoApplication_ApplyFrxParamsOverrides(object sender, ChoFrxParamsEventArgs e)
{
    e.GlobalApplicationSettings.SingleInstanceApp = true;
    e.MetaDataFilePathSettings.ConfigurationMetaDataFilePath = @"C:\Config\MetaDataFilePath.meta";
}

Cinchoo – Binary Downloads

Latest Cinchoo framework binary can be downloaded from

Nuget

Cinchoo is available as NuGet package

Goto Nuget console, use the below command

Install-Package Cinchoo

Please post your valuable feedback in this blog. Also if I miss anything, please email me at cinchoofrx@gmail.com. Glad to add them in future releases. Thanks for your support.

Cinchoo – Property Replacer, Part 2

Customizing Property Replacer – Dynamic Properties

We have learnt about defining and using static property in the previous section. In this section, I’m going to talk about defining and using Dynamic properties in your application. Dynamic properties are properties their values are resolved at the time of calling.

Below is the sample Property Replacer,

public class ChoDynamicPropertyReplacer : IChoKeyValuePropertyReplacer
{
    #region Instance Data Members

    private readonly Dictionary<string, string> _availPropeties = new Dictionary<string, string>()
        {
            { "HOME_PAGE", "Cinchoo framework website." },
            { "LAST_VISIT", "The last visit to the home page." }
        };

    #endregion Instance Data Members

    #region IChoKeyValuePropertyReplacer Members

    //Called by framework to see it is valid property replacer for the passed propertyName
    public bool ContainsProperty(string propertyName)
    {
        return _availPropeties.ContainsKey(propertyName);
    }

    //Yes it is, then framework calls this method to find the value for the property
    public string ReplaceProperty(string propertyName, string format)
    {
        switch (propertyName)
        {
            case "HOME_PAGE":
                return "http://www.cinchoo.com";
            case "LAST_VISIT":
                return ChoObject.Format(DateTime.Now, format);
            default:
                return null;
        }
    }

    //This method will be used to generate help
    public string GetPropertyDescription(string propertyName)
    {
        if (_availPropeties.ContainsKey(propertyName))
            return _availPropeties[propertyName];
        else
            return null;
    }

    #endregion

    #region IChoPropertyReplacer Members

    //Name of the property replacer
    public string Name
    {
        get { return "Dynamice Property Replacer"; }
    }

    //This property used to get list of supported properties and their descriptions
    public IEnumerable<KeyValuePair<string, string>> AvailablePropeties
    {
        get
        {
            foreach (KeyValuePair<string, string> keyValue in _availPropeties)
                yield return keyValue;
        }
    }

    #endregion
}

Here is the code to plug-in the above property replacer to the framework and using it.

static void Main(string[] args)
{
    ChoPropertyManagerSettings.Me.Add(new ChoDynamicPropertyReplacer());

    Console.WriteLine("Output of %%HOME_PAGE%% : %HOME_PAGE%".ExpandProperties());
    Console.WriteLine("Output of %%LAST_VISIT%% : %LAST_VISIT%".ExpandProperties());

    return;
}

When you run the above statements in an application, the output will be

Output of HOME_PAGE : http://www.cinchoo.com
Output of LAST_VISIT : 1/17/2012 1:07:23 PM

Hope this helps.

Cinchoo – Command Line Argument Parser, Part 14

Inside Command Line Argument Parser – Automatic Help

Another feature that Cinchoo framework provides you is that, it automatically creates Usage help text.

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

For a sample Command Line object below,

[ChoCommandLineArgObject(ApplicationName = "Hello World", Copyright = "Copyright (c) Cinchoo Inc.")]
public class MyCommandLineArgObject : ChoCommandLineArgObject
{
    [ChoCommandLineArg("D", DefaultValue = "%NOW%", Description = "Business Date.", ShortName = "Today")]
    public DateTime Today
    {
        get;
        set;
    }

    [ChoCommandLineArg("N", DefaultValue = "{100+100}", Description = "No of spaces for a TAB.")]
    public int Count
    {
        get;
        set;
    }

    [ChoCommandLineArg("I", DefaultValue = false, FallbackValue = true, Description = "Include Sub directory.")]
    public bool IncludeSubDir
    {
        get;
        set;
    }

    [ChoCommandLineArg("O", IsRequired = true, Description = "Output Directory.")]
    public string OutFolder
    {
        get;
        set;
    }
}

If the application containing above type executed with the below command line arguments

/help or /? or /h

When creating instance of MyCommandLineArgObject as below,

MyCommandLineArgObject commandLineArgObject = new MyCommandLineArgObject();
Console.WriteLine(commandLineArgObject.ToString());

And run the application, the commandLineArgObject will display the usage as below

Hello World [Version 1.0.0.0]
Copyright (c) Cinchoo Inc.

Cinchoo.Core.CommandLineArgs.Test.exe -D:Today -N:<int> -I:{True|False} -O:[<string>]

Where
        -D      Business Date.
        -N      No of spaces for a TAB.
        -I      Include Sub directory.
        -O      Output Directory.

Cinchoo – Command Line Argument Parser, Part 13

Inside Command Line Argument Parser – Load from file

In some cases, if you have multiple large, complex sets of command-line arguments that you want to run multiple times, Cinchoo framework provides a way to load the command line arguments from a file.

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

Let say you create a file c:\CmdLine.txt with the below command line arguments

N:{100+50} /I -O:C:\Log -D:%NOW%

Sample Command Line object below,

[ChoCommandLineArgObject(ApplicationName = "Hello World", Copyright = "Copyright (c) Cinchoo Inc.")]
public class MyCommandLineArgObject : ChoCommandLineArgObject
{
    [ChoCommandLineArg("D", DefaultValue = "%NOW%", Description = "Business Date.", ShortName = "Today")]
    public DateTime Today
    {
        get;
        set;
    }

    [ChoCommandLineArg("N", DefaultValue = "100", Description = "No of spaces for a TAB.")]
    public int Count
    {
        get;
        set;
    }

    [ChoCommandLineArg("I", DefaultValue = false, FallbackValue = true, Description = "Include Sub directory.")]
    public bool IncludeSubDir
    {
        get;
        set;
    }

    [ChoCommandLineArg("O", IsRequired = true, Description = "Output Directory.")]
    public string OutFolder
    {
        get;
        set;
    }
}

If the application containing above type executed with the below command line arguments (Please note, the file is been passed with @ symbol.)

@"C:\CmdLine.txt"

When creating instance of MyCommandLineArgObject as below,

MyCommandLineArgObject commandLineArgObject = new MyCommandLineArgObject();
Console.WriteLine(commandLineArgObject.ToString());

And run the application, the commandLineArgObject will contain the below values

Hello World [Version 1.0.0.0]
Copyright (c) Cinchoo Inc.

-- Cinchoo.Core.CommandLineArgs.Test.MyCommandLineArgObject Dump --
Today: 1/6/2012 12:32:00 PM
Count: 150
IncludeSubDir: True
OutFolder: C:\Log

Cinchoo – Property Replacer, Part 1

Customizing Property Replacer – Static Properties

If you need that you need to define and use list of your own properties for your requirements, you can do so by adding <key, value> pair to ChoStaticDictionaryPropertyReplacer class. There values are static, will not be evaluated for each time.  Cinchoo framework will take these values for property replacement in various places like Command Line parsing, Configuration Management, Object.ToString() etc.

Example, add CLUSTER_NAME property at the start of the application as below

ChoStaticDictionaryPropertyReplacer.Me.Add("CLUSTER_NAME", "CNYC100123023");

Then, you can use that property by using the below code

string msg = "Cluster Name is %CLUSTER_NAME%";
Console.WriteLine(ChoString.ExpandProperties(msg));

When you run the above statements in an application, the output will be

Cluster Name is CNYC100123023

Cinchoo – Command Line Argument Parser, Part 11

Inside Command Line Argument Parser – Available Properties

In here I’ll list all the available properties in Cinchoo framework.

Properties can be specified with % delimiters as below

%NOW%

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

Cinchoo framework allows you to pass property as command line argument. At run time, the framework replaces with property value.

Global Properties

Below are the list of global properties available for use as command line arguments

  • %APPLICATION_NAME% – Application Name.
  • %PROCESS_ID% – Process Identifier.
  • %THREAD_ID% – Thread Identifier.
  • %THREAD_NAME% – Managed .NET Thread Name, if any.
  • %RANDOM_NO% – Random integer value.
  • %TODAY% – Current Date.
  • %NOW% – The current date and time on this computer, expressed as the local time.

Environment Properties

Below are the list of Environment properties available for use as command line arguments

  • %CURRENT_DIRECTORY% – The fully qualified path of the current directory; that is, the directory from which this process starts.
  • %MACHINE_NAME% – The NetBIOS name of this local computer
  • %OS_VERSION% – An OperatingSystem object that contains the current platform identifier and version number.
  • %PROCESSOR_COUNT% – The number of processors on the current machine.
  • %SYSTEM_DIRECTORY% – The fully qualified path of the system directory.
  • %SYSTEM_PAGE_SIZE% – The amount of memory for an operating system’s page file.
  • %TICK_COUNT% – The number of milliseconds elapsed since the system started.
  • %USER_DOMAIN_NAME% – The network domain name associated with the current user.
  • %USER_NAME% – The user name of the person who is currently logged on to the Windows operating system.
  • %VERSION% – A Version object that describes the major, minor, build, and revision numbers of the common language runtime.
  • %WORKING_SET% – The amount of physical memory mapped to the process context.

Please visit PropertyReplacer section for more information about Property re-placers.