.NET自动服务程序—C#(二)

类别:.NET开发 点击:0 评论:0 推荐:

.NET自动服务程序—C#(二)

追风(VisualSW)

启动服务:

定义两个变量:

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自动帮你生成安装程序。

打开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()

{}

 

本文地址:http://com.8s8s.com/it/it45166.htm