Free Tools   Source Code   Electronic   Ideas? 
XwServiceBase 1.0.0.1

You can use this to create windows services.

As we all know, sometimes we have the necessity of running tasks at predefined times. Windows has "Schedule tasks", and they are very useful and reliable.

However i always find my self grinding my teeth when using them because i don't want to make a script or command line for every little thing.

 If you are anything like me, you like Windows Services.

This code, is a code base that i use every time i need a new service.
I just copy the solution, and change its name. Instant service!

What this gives me is:

- Multiple threads configured on the app.config

- Individual log files for each thread (errors and messages)
- Possibility of sending errors by mail
- In debug (under Visual Studio or not) runs as console application
- In Release runs as Service
- It can run as Console Application in release mode when started with "-c" switch
- Self Install and uninstall, avoiding external tools  

Here's a example of the output:

*******************************************************************************
XWega Service Base                                           V:1.0.0.1
===============================================================================
WARNING: this is a debug version
===============================================================================
-------------------------------------------------------------------------------
29-08-2010 20:30:51.2964 => SVR : Service Started (Debug)
29-08-2010 20:30:51.3214 => Thread1          : STARTING...
29-08-2010 20:30:51.3214 => Thread1          : do your stuff
29-08-2010 20:30:51.3264 => Thread1 : ...DONE!

Here the possible command switch (shown by "-h")
*******************************************************************************
XWega Service Base                                           V:1.0.0.1
===============================================================================
HELP:
-c : Run as console application
-i : Install service
-u : Uninstall service

Example of the app.config:
<appThreads enabled="true" abortTimeout="00:10:00">
<thread enabled="true" runOnStart="true" recurse="true" time="00:00:10"  name="Thread1" method="Thread1.GO" workingFolder="" />
<!--thread enabled="false" runOnStart="true" recurse="true" time="00:01:00" name="Thread2" method="Thread2.GO" workingFolder="" /-->
<!--thread enabled="false" runOnStart="true" recurse="true" time="00:01:00" name="Thread3" method="Thread3.GO" workingFolder="" /--> </appThreads>
Notes on this configuration:
- enable: self explanatory

- runonstart: when true, the thread will run when the service is started,
when false it will wait for the designated time to run

- recurse: if true, the time value will be the wait period between the thread 
exits and get started again. If false, time is an absolute value indicating the
time of day for the thread to run.
The same Thread will never start if the previous run has not ended yet.

- name: Name of the thread, this will be used to five a name to the log
files, and identify the error mails.

- method: the c# method that will be started, and that receives the 
AppThreadStruct representing the thread
- workingFolder: this is accessible in the AppThreadStruct and can be used to provide the thread with a disk or network path where it should take care of its business.

One thread has the following base:
using System;
using System.Threading;
using XwServiceBaseSrv;

class Thread1
{
     public void GO(AppThreadStruct thread)
    {
        try
        {
            Srv.LogThread(LogLevel.Debug1, thread.Settings, "STARTING..."nullfalse);
            
            //YOUR CODE HERE!

            Srv.LogThread(LogLevel.Debug1, thread.Settings, "...DONE!"nullfalse);          
        }
        catch (Exception ex)
        {
            Srv.LogThread(LogLevel.Error, thread.Settings, "GO", ex, false);
        }
    }
}



 

Support
Contact
Settings
Logout
Copyright © XWega, 2010. All Rights Reserved.