如何完成一个实现Pause和Continue这两个功能的Windows Service

如何实现一个Windows Service,我以前有篇文章进行介绍过,具体地址如下,如果想做这方面程序的话,可以先看它。

http://blog.csdn.net/knight94/archive/2006/03/17/627298.aspx

 

但是对于一个Windows Service来说,经常是24小时运行,那么难免有时会进行类似于PauseContinueStop以及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();

        }

 

把如上的修改操作在原先的例子上,一个能PauseContinueService就完成了。

 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
一个Windows Service Wrapper与批处理文件的例子是使用Apache Commons Daemon的Procrun来封装一个Java应用程序作为Windows服务运行。以下是具体步骤: 1. 下载并解压缩Apache Commons Daemon的二进制文件 2. 创建一个批处理文件,命名为“install.bat”,并将以下命令添加到文件中: ``` @echo off set SERVICE_NAME=MyService set PR_INSTALL=C:\path\to\commons-daemon-1.2.2-bin-windows\prunsrv.exe set PR_DESCRIPTION=My Service Description set PR_DISPLAYNAME=My Service Display Name set PR_LOGPREFIX=MyService set PR_LOGPATH=C:\path\to\logs set PR_STDOUTPUT=C:\path\to\logs\stdout.log set PR_STDERROR=C:\path\to\logs\stderr.log set PR_CLASSPATH=C:\path\to\my\jar\file.jar set PR_STARTCLASS=com.example.MyServiceMainClass set PR_STARTMETHOD=start set PR_STOPCLASS=com.example.MyServiceMainClass set PR_STOPMETHOD=stop set PR_JVM=auto set PR_JVMMS=256 set PR_JVMMX=1024 set PR_JVMSS=4000 set PR_JVMOPTIONS=-Dfile.encoding=UTF-8 %PR_INSTALL% //IS//%SERVICE_NAME% ^ --DisplayName="%PR_DISPLAYNAME%" ^ --Install="%PR_INSTALL%" ^ --LogPath="%PR_LOGPATH%" ^ --LogPrefix="%PR_LOGPREFIX%" ^ --StdOutput="%PR_STDOUTPUT%" ^ --StdError="%PR_STDERROR%" ^ --Jvm="%PR_JVM%" ^ --JvmMs="%PR_JVMMS%" ^ --JvmMx="%PR_JVMMX%" ^ --JvmSs="%PR_JVMSS%" ^ --Classpath="%PR_CLASSPATH%" ^ --StartClass="%PR_STARTCLASS%" ^ --StartMethod="%PR_STARTMETHOD%" ^ --StopClass="%PR_STOPCLASS%" ^ --StopMethod="%PR_STOPMETHOD%" ^ --Description="%PR_DESCRIPTION%" ^ ++JvmOptions=%PR_JVMOPTIONS% pause ``` 3. 将上述命令中的参数替换为您的应用程序的实际值,并运行install.bat文件,以安装服务。 4. 创建一个批处理文件,命名为“uninstall.bat”,并将以下命令添加到文件中: ``` @echo off set SERVICE_NAME=MyService set PR_INSTALL=C:\path\to\commons-daemon-1.2.2-bin-windows\prunsrv.exe %PR_INSTALL% //DS//%SERVICE_NAME% pause ``` 5. 运行uninstall.bat文件,以卸载服务。 通过以上步骤,您的Java应用程序现在可以作为Windows服务运行,并使用Procrun作为Windows Service Wrapper。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值