我现在在做一个windows service 实现一个合同的指定天数提醒,这个合同在一个用asp.net和c#实现的web网站中。我是通过在windows服务中用一个timer3秒钟去读数据库中设定的时间,如果设定的时间和我现在的时间相等,发通知提醒合同即将到期。
service1.cs中的代码如下:
  运行时就是出现如下的错误:无法将文件“obj\x86\Debug\KYPT.exe”复制到“bin\Debug\KYPT.exe”。文件“bin\Debug\KYPT.exe”正由另一进程使用,因此该进程无法访问此文件
  我在计算机管理-》服务中把这个服务关掉。提示我需要开启服务。我要是开启服务就会出现上面的错误。但是我的服务时必须要开启的要不让不会定时从数据库中读数据啊  哪位大侠给解决一下!万分感谢!
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Model;
using service;
using System.Data.SqlClient;namespace KYPT
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }        protected override void OnStart(string[] args)
        {
            timer1.Interval = 1000;
            timer1.Enabled = true;
        }        protected override void OnStop()
        {
            timer1.Enabled = false;
            timer1.Stop();
        }        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timer1.Enabled = false;
            InformSrv bs = new InformSrv();
            List<InformInfo> listcontact = bs.getremindertime();
            listcontact.ForEach(c =>
            {
                DateTime dt1 = DateTime.Now;
                DateTime dt2 = Convert.ToDateTime(c.ProRemindTime);
                TimeSpan span = dt1 - dt2;
                BaseSrv<InformInfo> inform = new BaseSrv<InformInfo>();
                if (span.TotalSeconds == 0)
                {
                    InformInfo inf = new InformInfo();
                    inf.PTypeName = c.PTypeName;
                    inf.PType = c.PType;
                    inf.IsApply = 1;
                    inform.add(inf);                }
            });
            timer1.Enabled = true;
        }
    }
}

解决方案 »

  1.   

    类似于合同到期这种一天只看一次的业务没必要3秒种就轮询一次吧?代价太大了。根据我的项目实践,你这种业务没有必要单独起个windows服务,直接在IIS里开个线程就OK了。asp.net使用的bin是随时可以被替换的。其后果就是重启网站而已。
      

  2.   

    服务的启动方式不一样啊,必须先停止服务,才可以替換文件,然后再启动服务。
    另外: timer1_Elapsed 的最后一句 timer1.Enabled = true 有问题,应去掉。