using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Windows.Forms;
namespace WindowsService1
{
public class Service1 : System.ServiceProcess.ServiceBase
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public Service1()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer();
this.timer1.Tick += new EventHandler(Timer_Ticker);
this.ServiceName = "kgds_status_process";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
this.timer1.Interval = 1000;
this.timer1.Start();
FileStream fs = new FileStream(@"c:\temp\status_process_log.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("kgds_status_process: service started");
m_streamWriter.Flush();
m_streamWriter.Close();
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
FileStream fs = new FileStream(@"c:\temp\status_process_log.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("kgds_status_process: service stopped");
m_streamWriter.Flush();
m_streamWriter.Close();
this.timer1.Stop();
}
private void Timer_Ticker (object sender, System.EventArgs e)
{
FileStream fs = new FileStream(@"c:\temp\status_process_log.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine("tick - " + DateTime.Now.ToString());
m_streamWriter.Flush();
m_streamWriter.Close();
}
}
}