.NET自动服务程序—C#

应用程序开发中,常常需要实现这样一种功能:让服务器在每天的特定时刻运行固定的程序(或者实现固定的操作),比如让系统在每天的200备份数据库数据。要实现这样的功能,我们可以使用Windows服务(Windows service)。

Windows service是一种系统自动的、无人值守的程序(仅存在于Windows NT2000XP操作系统中),它能够在系统启动时开始运行。用户可以通过Service Control Manager (SCM) Applet或者一些特殊的service-control应用来访问Windows service,使服务在没有用户登录到系统之前执行。

        .NET出现以前,编写Windows服务是VC++Delphi才能办到的事情,

VB必须使用第三方控件才可以办到,而且编写起来特别的复杂。使用Microsoft®.NET Framework,我们可通过创建作为服务安装的应用程序来方便地创建Windows服务。

 

设计:

一个Windows服务程序,按照配置文件中的配置,在指定时刻运行指定程序。

流程:

启动服务à读取配置文件à启动定时器

定时器定时触发(比如每隔30)à循环需要运行组件时间à时间到à运行指定程序

编写:

创建一个Windows Service

ASPectratio="t">

Server1.cs更名为SchedulerServer.cs

双击SchedulerServer.cs打开设计页面,从工具栏的组件中拖Timer控件。

更名为SchedulerTimer,并设置Enabledflase

注意必须是Components里面的Timer控件,Windows Forms里面的Timer控件不行。

 

F7浏览代码可以看到如下代码

 

服务器启动的时候运行:

/// <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.

}

 

服务停止的时候运行:

/// <summary>

/// Stop this service.

/// </summary>

protected override void OnStop()

{

        // TODO: Add code here to perform any tear-down necessary to stop your service.

}

 

添加写日志函数,用来记录服务的日志:

public static void WriteLog(string strLog)

                {

                        string strPath;

                        strPath=System.Environment.SystemDirectory;

                        strPath+=@"\SchedulerServer.txt";

                        FileStream fs = new FileStream(strPath,FileMode.OpenOrCreate,FileAccess.Write);

                        StreamWriter m_streamWriter = new StreamWriter(fs);

                        m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

                        m_streamWriter.WriteLine(strLog);                       

                        m_streamWriter.Flush();

                        m_streamWriter.Close();

                        fs.Close();

                }

 

 

自动服务配置文件

配置文件保存在系统目录,Windows 2000WinNT\System32,文件名为SchedulerServer.xml,存储了自动服务的所有配置信息,下面我们来看SchedulerServer.xml的格式:

<SchedulerServer>

        <AutoServer>

                <FilePath>C:\AutoBackup.dll</FilePath>

                <RunTime>02:00</RunTime>

        </AutoServer>

        <AutoServer>

                <FilePath>D:\AutoMail.dll</FilePath>

                <RunTime>03:00</RunTime>

        </AutoServer>

</SchedulerServer>

 

FilePath设置需要运行的组件的路径

RunTime设置需要运行的时间

如果有多个程序需要运行,只需要添加<AutoServer>节点

 

程序中添加读取配置文件函数:

private bool ReadConf()

                {

                        try

                        {

                                string strPath;

                                XmlDocument xmldoc=new XmlDocument();

                                XmlNodeList xmlnd;

 

                                strPath=System.Environment.SystemDirectory+@"\SchedulerServer.xml";

                                xmldoc.Load(strPath);

                                xmlnd=xmldoc.SelectNodes("SchedulerServer/AutoServer");

                                arrConf=new String[2,xmlnd.Count];

                               

                                for(int i=0;i<xmlnd.Count;i++)

                                {

                                        arrConf[0,i]=xmlnd[i].SelectSingleNode("FilePath").InnerXml.Trim();

                                        arrConf[1,i]=xmlnd[i].SelectSingleNode("RunTime").InnerXml.Trim();

                                }

                                return true;

                        }

                        catch(Exception e)

                        {

                                WriteLog(DateTime.Now.ToString());

                                WriteLog("Read Configuration Error:");

                                WriteLog(e.ToString());

                                return false;

                        }

 

                }

启动服务:

定义两个变量:

private string[,] arrConf;保存配置信息

private Assembly[] assObj;加载组件

 

OnStart事件中添加如下代码:

protected override void OnStart(string[] args)

{                     

WriteLog("/************************************************************/");

        WriteLog("ScheculerServer Start at "+DateTime.Now.ToString());

        //Load Configuation

        if(!ReadConf())return;

 

        //Load Assembly

        try

        {

                assObj=new Assembly[arrConf.GetLength(1)];

                for(int i=0;i<assObj.Length;i++)

                {

                        assObj[i]=Assembly.LoadFrom(arrConf[0,i].ToString());

                        arrConf[0,i]="NotRuning";

                }

        }

        catch(Exception e)

        {

                WriteLog(DateTime.Now.ToString());

                WriteLog("Load Dll Error:");

                WriteLog(e.ToString());

        }

 

        //Start Time

        SchedulerTimer.Interval=30000;//设置每30秒触发

        SchedulerTimer.Enabled=true;//启动定时器

}

 

定时器触发:

此处完成检查时间是否运行

private void SchedulerTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

        try

        {

                //SchedulerTimer.Enabled=false;

                DateTime dtNow=DateTime.Now;

                DateTime dtRun;

                for(int i=0;i<arrConf.GetLength(1);i++)

                {

                        dtRun=Convert.ToDateTime(arrConf[1,i].ToString());

                        if(dtRun.AddSeconds(-30)<=dtNow && dtNow<=dtRun.AddSeconds(30))                                        {

                                if(arrConf[0,i].ToString().Trim()=="NotRuning")

                                {

                                        foreach(Type t in assObj[i].GetTypes())

                                        {

                                                if(t.IsClass && !t.IsAbstract && t.IsPublic)

                                        {

                                                        Object obj=Activator.CreateInstance(t);                        

                                                        MethodInfo mi=t.GetMethod("Run");

                                                        if(mi!=null)

                                                        {

                                                                mi.Invoke(obj,null);

                                                                obj=null;

                                                                GC.Collect();

                                                                break;

                                                        }

                                                        obj=null;

                                                        GC.Collect();                                                     

                                                }

                                        }

                                        arrConf[0,i]="OnRuning";                                             

                WriteLog("-------------------------------------------------------------");

                WriteLog(DateTime.Now.ToString()+":Runing "+arrConf[1,i]+" "+assObj[i].Location.ToString());

                WriteLog("-------------------------------------------------------------");                                                      

                        }

                       

                }

        catch(Exception ex)

        {

                                WriteLog("##########################################################");

        WriteLog(ex.ToString());

                                WriteLog("##########################################################");

        }

}

 

这时程序的主骨架就完成了,下一步需要制作Windows服务安装程序,切换到设计页面,在属性的右下脚有Add Installer字样,单击,VS.net自动帮你生成安装程序。

ASPectratio="t" v:ext="edit">

打开ProjectInstaller.cs文件,可以看到两个组件,设置其属性。

运行帐号选择LocalSystem,系统以本地系统帐号运行。

启动类型选择自动。

编译为exe,打开VS.NET cmd,进入exe目录,运行InstallUtil.exe SchedulerServer.exe安装服务,成功之后,打开服务管理可以看到刚才的服务,启动服务即可。

服务日志保存在系统目录下的SchedulerServer.txt

/************************************************************/

ScheculerServer Start at 12/24/2003 3:46:21 PM

-------------------------------------------------------------

12/24/2003 3:46:51 PM:Runing 15:47 e:\work\geid\src\geidautocheck\bin\debug\geidautocheck.dll

-------------------------------------------------------------

ScheculerServer Stop at 12/24/2003 3:48:55 PM

/************************************************************/

 

自动运行程序:

1.NET组件(dll)

2.入口为

public void Run()

{}

posted @ 2011-05-16 10:46  还是你最好  阅读(677)  评论(0编辑  收藏  举报