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

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

Decorating the above class with RunInstallerAttribute 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(new AppHost(), 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

[RunInstaller(true)]
public class AppHost : ChoApplicationHost, IChoWinFormApp
{
    MainForm form = new MainForm();
    public AppHost()
    {
    }

    public Form MainFormWindow
    {
        get { return form; }
    }

    public ContextMenu GetContextMenu(ContextMenu contextMenu)
    {
        //Build the context menu items
        return contextMenu;
    }

    public string TooltipText
    {
        get { return null; }
    }

    public System.Drawing.Icon TrayIcon
    {
        get { return null; }
    }

    public string BalloonTipText
    {
        get { return null; }
    }

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

Decorating the above class with RunInstallerAttribute 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(new AppHost(), args);
    }
}

Thats all. Try it.


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

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

Make sure the type is decorated with RunInstallerAttribute. 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(new AppHost(), args);
    }
}

That’s all, you application is self install ready 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

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.

[RunInstaller(true)]
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 AppSettings in App.config file as below

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="CinchooFrxConfigPath" value="C:\Config\CoreFrx.config"/>
  </appSettings>
</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

Developers, please bookmark this page. I’ll keep updating the state of the framework in this post.

Mar 09, 2014 / v1.0.3.9 (Latest)

Cinchoo.Framework.Dlls.Zip (Any CPU)

  • Fixed race condition in creating and managing individual Configuration Section files.

Feb 13, 2014 / v1.0.3.8

Cinchoo.Framework.Dlls.Zip (Any CPU)

  • Fixed the issue related to turning on/off Framework logging completely using ChoSwitch (System.Diagnostics.Switch). in App.Config as below. Possible switch are Off, Verbose, Info, Warning, Error.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<system.diagnostics>
		<switches>
			<add name="ChoSwitch" value="Off" />
		</switches>
	</system.diagnostics>
</configuration>

Feb 03, 2014 / v1.0.3.7

Cinchoo.Framework.Dlls.Zip (Any CPU)

  • Outstanding issues related to disabling logging completely using TraceLevel switch.
  • Added optional Cinchoo.Core.Serializers.dll (a pre-generated and pre-compiled XmlSerializers to boost performance).

Jan 31, 2014 / v1.0.3.6

Cinchoo.Framework.Dlls.Zip (Any CPU)

  • Able to turn off Cinchoo logging completely by setting LogSettings..TraceLevel = TraceLevel.Off

Jan 22, 2014 / v1.0.3.4

Cinchoo.Framework.Dlls.Zip (Any CPU)

  • Fixed some minor issues.

Jan 12, 2014 / v1.0.3.3

Cinchoo.Framework.Dlls.Zip (Any CPU)

  • Fixed an issue (Root element is missing), reported by user using the library in locale environment. 

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 – Command Line Argument Parser, Part 12

Inside Command Line Argument Parser – Expression Evaluator

In some cases, you may want to pass some expression as command line arguments and assign them to command line arguments. Cinchoo framework covers this requirement as well.

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

Let say, you want to pass 100 + 50 as command line argument (N), you can do so as below

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

Expressions are given in { and } braces. Please note that the symbols needs to have spaces

Cinchoo framework looks into the {100+50} command line argument value, evaluates and assigns the outcome value to corresponding member (Count).

[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;
    }

    [ChoDefaultCommandLineArg(IsRequired = true, Description = "Semi-colon seperated list of folders")]
    [ChoTypeConverter(typeof(ChoStringToStringArrayConverter))]
    public string[] Folders
    {
        get;
        set;
    }
}

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

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

Or

N:"{100 + 50}"/I -O:C:\Log -D:%NOW%
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

Please visit ExpressionEvaluator section for more detailed information about expressions.


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

Follow

Get every new post delivered to your Inbox.