C#制作Windows service服务系列二:演示一个定期执行的windows服务及调试(windows service)...

  系列一: 制作一个可安装、可启动、可停止、可卸载的Windows service(downmoon原创)

  系列二:演示一个定期执行的windows服务及调试(windows service)(downmoon)

  系列三: windows service系列三--制作可控制界面的windows service

 

   一、经常有人问起如何让程序定期自动执行
  除了像系统任务和SQL JOB/DTS等都可以满足不同的用户需求外,这里演示了如何做一个简单的windows serivce的框架。主要的功能是按照预先设定的时间间隔来执行类似的操作,比如抓取网页,抓取天气预报,群发邮件等。

  假定用户有如下代码需要定期自动执行:

ContractedBlock.gif ExpandedBlockStart.gif Code
 1ContractedBlock.gifExpandedBlockStart.gif 获取内容#region 获取内容
 2ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 3InBlock.gif        /// 获取内容,by tony 2009.9,16 
 4InBlock.gif        /// 邀月(downmoon):3w@live.cn
 5ExpandedSubBlockEnd.gif        /// </summary> 

 6InBlock.gif        public void GetContentByMethods(bool isDelteOldFile)
 7ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 8InBlock.gif            System.Text.StringBuilder builder = new System.Text.StringBuilder();
 9InBlock.gif
10InBlock.gif            try
11ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
12ContractedSubBlock.gifExpandedSubBlockStart.gif                DeleteOldFile#region DeleteOldFile
13InBlock.gif                if (isDelteOldFile)
14ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
15InBlock.gif                    string path = Globals.BakPath;
16InBlock.gif                    if (System.IO.Directory.Exists(path))
17ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
18InBlock.gif                        string[] strFiles = System.IO.Directory.GetFiles(path);
19InBlock.gif                        foreach (string strFile in strFiles)
20ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
21InBlock.gif                            System.IO.File.Delete(strFile);
22ExpandedSubBlockEnd.gif                        }

23ExpandedSubBlockEnd.gif                    }

24ExpandedSubBlockEnd.gif                }

25ExpandedSubBlockEnd.gif                #endregion

26ContractedSubBlock.gifExpandedSubBlockStart.gif                My Methods#region My Methods
27InBlock.gif                int tempCount = 0;
28InBlock.gif                string temp = "Operation_{0} was  sucessfully executed at {1} by Operator_{2}!";
29InBlock.gif                string temp2 = string.Empty;
30InBlock.gif                string filename = Globals.CreateXMLDir(Globals.BakPath) + Globals.StrTempDate + ".txt";
31InBlock.gif                for (int k = 0; k < Globals.TopCount; k++)
32ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
33InBlock.gif                    temp2 = string.Empty;
34InBlock.gif                    //此处从数据表中读取数据,做一些事情,比如发邮件,抓取网页内容等。
35InBlock.gif                    temp2 = string.Format(temp, k, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), k) + "\r\n";
36InBlock.gif                    builder.Append(temp2);
37InBlock.gif                    tempCount++;
38ExpandedSubBlockEnd.gif                }

39InBlock.gif                builder.Append("total [" + tempCount + "] records was executed this time!");
40InBlock.gif                SaveFileResult(filename, builder.ToString());
41ExpandedSubBlockEnd.gif                #endregion

42ExpandedSubBlockEnd.gif            }

43ExpandedSubBlockStart.gifContractedSubBlock.gif            catch (Exception ex) dot.gifstring s = ex.Message; }
44ExpandedBlockEnd.gif        }

  那么,只需要将以上代码放在windows service环境下,部署成windows service,即可定期执行
  详细过程我就省略了! 看文后源码
  说明:
  1、SetupServer.bat ,双击可安装服务
  2、unInstallServer.bat,双击可卸载服务
  3、主要文件WSDemoByTime.exe
  4、配置文件WSDemoByTime.exe.config(主要配置数量、间隔时间、数据连接等)
  运行效果,这里只演示了生成一个基本的记录文件。见图;
  邀月工作室

  邀月工作室
  

  二、如何调试windows服务(windows service)。

  这里提供简单的处理过程:

  假定,我们已经部署好了windows service,结果发现不能如我们想像的运行,除了像本系列所描述的记录日志外,还可以直接调试进程。

  具体步骤见下图:

   一、调试--附加到进程

  邀月工作室
  邀月工作室

  二、设置断点,调试
  邀月工作室
  三、如何修改windows service配置并生效?

      请注意,windows service的配置文件一般形如XXX.exe.Config,在本文中是WSDemoByTime.exe.Config,这个配置文件必须与WSDemoByTime.exe文件一致,并且修改配置文件后,需要重新启动windows serice才能使配置生效。
  邀月工作室

  最后,附上源码,转载请注明出处,邀月 3w@live.cn 谢谢!

  源码下载

转载于:https://www.cnblogs.com/downmoon/archive/2009/09/16/1567643.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值