Kate
Kate

Reputation: 372

Service not run again

i have windows service. The program jump to getFileList, after that jump to DeleteOldBackupFiles and after that nothing...the program not run again and I dont know why..Have you any idea? Example of my code:

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();
        }
    }               
}

I want after DeleteOldBackupFiles, run OnStart again...

Upvotes: 0

Views: 68

Answers (1)

Yair Nevet
Yair Nevet

Reputation: 13003

This is your problem:

 timer.AutoReset = false;

Delete it or set it to true (default) because that's mean your event-handler will be called only once.

Update: If that does not help you, than I would suggust you to debug it using: Debugger.Launch();

Upvotes: 1

Related Questions