服务不再运行

时间:2013-09-13 08:02:35

标签: c# windows-services

我有Windows服务。程序跳转到getFileList,之后跳转到DeleteOldBackupFiles,然后没有...程序没​​有再次运行,我不知道为什么......有什么想法吗? 我的代码示例:

protected override void OnStart(string[] args)
{
    timer = new System.Timers.Timer();
    timer.Elapsed += new ElapsedEventHandler(getFileList);
    timer.Interval = 10000;
    timer.AutoReset = true;
    timer.Start();
}

protected override void OnStop()
{
}

private void getFileList(object sender, EventArgs e)
{  
    if (!Directory.Exists(backup))
    {
         Directory.CreateDirectory(backup);
    } 

    List<string> files = new List<string>();
    try
    {
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(*****);
        request.Method = WebRequestMethods.Ftp.ListDirectory;
        request.Credentials = new NetworkCredential(***, ***);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        while (!reader.EndOfStream)
        {
            files.Add(reader.ReadLine());
        }

        files.Remove(".");
        files.Remove("..");
        reader.Close();

        responseStream.Close();
        response.Close();
    }
    catch (Exception)
    {
        //getFileList();                
    }

    if (files.Count != 0)
    {
        //timer.Stop();
        foreach (string file in files)
        {    
            //Work with XML file and give data to sql
        }
    }

    DeleteOldBackupFiles();
}

private void DeleteOldBackupFiles()
{
    string[] Oldfiles = Directory.GetFiles(backup);
    foreach (string Ofile in Oldfiles)
    {
        FileInfo fi = new FileInfo(Ofile);
        if (fi.LastWriteTime < DateTime.Now.AddMonths(-2))
        {
            fi.Delete();
        }
    }               
}

我想在DeleteOldBackupFiles之后再次运行OnStart ......

1 个答案:

答案 0 :(得分:1)

这是你的问题:

 timer.AutoReset = false;

删除它或将其设置为true(默认值),因为这意味着您的事件处理程序将仅被称为 一次。

<强>更新 如果这对您没有帮助,那么我会建议您使用:Debugger.Launch();

进行调试