如何实现一个Windows Service,我以前有篇文章进行介绍过,具体地址如下,如果想做这方面程序的话,可以先看它。
http://blog.csdn.net/knight94/archive/2006/03/17/627298.aspx
但是对于一个Windows Service来说,经常是24小时运行,那么难免有时会进行类似于Pause,Continue,Stop以及Restart之类的操作。也就是如何让自己写得Windows Service也能完成类似功能。
那么首先分析,对于一个24小时运行的Service来说,必定有子线程的支撑,那么对于Service的相应操作要转化到子线程中,那么我原先的文章对于此处有些问题,在此用一个更好的方法来更新它。
首先,介绍子线程类,完整代码如下。
//--------------------------- Sub-thread class ---------------------------------------
//------------------------------------------------------------------------------------
//---File: clsSubThread
//---Description: The sub-thread class file to demenstrate howto suspend or resume
// a thread
//---Author: Knight
//---Date: Aug.21, 2006
//------------------------------------------------------------------------------------
//---------------------------{Sub-thread class}---------------------------------------
namespace WinSDemo
{
using System;
using System.Threading;
using System.IO;
/// <summary>
/// Summary description for clsSubThread.
/// </summary>
public class clsSubThread
{
private Thread thdSubThread = null;
private Mutex mUnique = new Mutex();
private bool blnIsStopped;
private bool blnSuspended;
private bool blnStarted;
public bool IsStopped
{
get{ return blnIsStopped; }
}
public bool IsSuspended
{
get{ return blnSuspended; }
}
public clsSubThread()
{
//
// TODO: Add constructor logic here
//
blnIsStopped = true;
blnSuspended = false;
blnStarted = false;
}
/// <summary>
/// Start sub-thread
/// </summary>
public void Start()
{
if( !blnStarted )
{
thdSubThread = new Thread( new ThreadStart( SubThread ) );
blnIsStopped = false;
blnStarted = true;
thdSubThread.Start();
}
}
/// <summary>
/// Thread entry function
/// </summary>
private void SubThread()
{
do
{
// Wait for resume-command if got suspend-command here
mUnique.WaitOne();
mUnique.ReleaseMutex();
try
{
using( StreamWriter sw = new StreamWriter("c:\\MyLog.txt",true) )
{
sw.WriteLine( DateTime.Now.ToString() );
sw.Close();
}
}
catch{};
Thread.Sleep(1000);
}while( blnIsStopped == false );
}
/// <summary>
/// Suspend sub-thread
/// </summary>
public void Suspend()
{
if( blnStarted && !blnSuspended )
{
blnSuspended = true;
mUnique.WaitOne();
}
}
/// <summary>
/// Resume sub-thread
/// </summary>
public void Resume()
{
if( blnStarted && blnSuspended )
{
blnSuspended = false;
mUnique.ReleaseMutex();
}
}
/// <summary>
/// Stop sub-thread
/// </summary>
public void Stop()
{
if( blnStarted )
{
if( blnSuspended )
Resume();
blnStarted = false;
blnIsStopped = true;
thdSubThread.Join();
}
}
}
}
接着就是要修改Windows程序中的调用。
修改一,定义局部成员;
private clsSubThread mySubThread = null;
修改二,在InitializeComponent中初始化局部成员;
// Init sub-thread object here
mySubThread = new clsSubThread();
其次,就是Service定义的事件方法;
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
mySubThread.Start();
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
mySubThread.Stop();
}
protected override void OnPause()
{
mySubThread.Suspend();
}
protected override void OnContinue()
{
mySubThread.Resume();
}
把如上的修改操作在原先的例子上,一个能Pause和Continue的Service就完成了。