• Can anyone post a working code example of a Windows Service? I tried using the code in the article and had to play with it a bit to get it to compile. I then added an installer project and eventually installed the service in Windows. My service starts then stops and I get the message that "your service started then stopped..."

    Here is what I have:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Diagnostics;

    using System.Linq;

    using System.ServiceProcess;

    using System.Text;

    using System.Data.SqlClient;

    using System.Xml;

    using System.IO;

    namespace svcbroker

    {

    public partial class Service1 : ServiceBase

    {

    private bool m_Terminate = false;

    private string LogFolder = "c:\\svcbroker\\LogFolder\\";

    private string ArchiveFolder = "c:\\svcbroker\\ArchiveFolder\\";

    public Service1()

    {

    InitializeComponent();

    }

    protected override void OnStart(string[] args)

    {

    using (SqlConnection Conn = new SqlConnection("Data Source=(local);Initial Catalog=ServiceBroker;Integrated Security=SSPI"))

    {

    Conn.Open();

    using (SqlCommand Cmd = Conn.CreateCommand())

    {

    Cmd.CommandText = "ProcessLogQueue";

    Cmd.CommandType = System.Data.CommandType.StoredProcedure;

    Cmd.CommandTimeout = 0; // no timeout

    while (!this.m_Terminate) // looping until the service is stopped

    {

    string Response = Cmd.ExecuteScalar().ToString(); // execute the command

    if (Response.Length > 0)

    {

    XmlDocument Doc = new XmlDocument();

    Doc.LoadXml(Response);

    XmlNode RootNode = Doc.SelectSingleNode("LOG");

    XmlNode RowNode = RootNode.SelectSingleNode("Row");

    string ProcessName = RowNode.SelectSingleNode("ProcessName").InnerText;

    string MachineName = RowNode.SelectSingleNode("MachineName").InnerText;

    string MachineFolder = Path.Combine(this.LogFolder, MachineName);

    if (!Directory.Exists(MachineFolder))

    {

    try

    {

    Directory.CreateDirectory(MachineFolder);

    }

    catch (Exception Ex)

    {

    // log the failure to a logfile for the Windows Service

    return;

    }

    }

    // create the name of the log file

    string FileName = Path.Combine(MachineFolder, ProcessName + ".log");

    try

    {

    this.CheckLog(MachineName, FileName); // does the log file nee to be archived?

    }

    catch (Exception Ex)

    {

    // log the failure to a logfile for the Windows Service

    return;

    }

    try

    {

    using (StreamWriter SW = new StreamWriter(FileName, true))

    {

    SW.WriteLine(string.Format("{0} {1}", RowNode.SelectSingleNode("LogTime").InnerText,

    RowNode.SelectSingleNode("ProcessMessage").InnerText));

    }

    }

    catch (SqlException Ex)

    {

    // log the failure to a logfile for the Windows Service and quit

    }

    catch (System.Threading.ThreadAbortException)

    {

    // we have been ordered to quit

    }

    catch (Exception Ex)

    {

    // log the failure to a logfile for the Windows Service and quit

    }

    }

    }

    }

    }

    }

    private void CheckLog ( string MachineName, string FileName )

    {

    FileInfo FI = new FileInfo ( FileName );

    if ( FI.Exists )

    {

    DateTime Today = Convert.ToDateTime ( DateTime.Now.ToShortDateString () );

    if ( FI.LastWriteTime < Today )

    {

    string ArchiveFileName = Path.GetFileName ( FileName ).Replace ( ".log", FI.LastWriteTime.ToString ( "yyyyMMdd" ) + ".log" );

    string MachineFolder = Path.Combine ( this.ArchiveFolder, MachineName );

    if ( !Directory.Exists ( MachineFolder ) )

    {

    Directory.CreateDirectory ( MachineFolder );

    }

    FI.MoveTo ( Path.Combine ( MachineFolder, ArchiveFileName ) );

    }

    }

    }

    protected override void OnStop()

    {

    }

    }

    }