0

I wanted to create a service which runs automatically on everyday at 4.00 am, and that method would delete some files automatically when it is called, i have written the code but my method which i am calling in service is not getting executed. Below i have attached my code , please somebody suggest me a solution.

public partial class Scheduler : ServiceBase
{
    System.Timers.Timer _timer;
    public Scheduler()
    {
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
        try
        {
            _timer = new System.Timers.Timer();
            _timer.Interval = 1 * 60 * 1000;//Every one minute
            _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            _timer.Start();
        }
        catch (Exception ex)
        {
            Library.WriteErrorLog(ex);
        }
    }
    public void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Library.WriteErrorLog("Timer elapsed");    
        this.ExtractDataFromSharePoint();      
    }    
    public void ExtractDataFromSharePoint()
    {
        Library.WriteErrorLog("inside task");    
        ArchiveAdministration admin = new ArchiveAdministration();
        admin.ArchiveAuto();
        Library.WriteErrorLog("job completed");
        _timer.Stop();   
    }
}

And my log file has only the following output statements:

Timer Elapsed
inside task

I am not getting " Job completed " statement which i have included..

Ben
  • 763
  • 1
  • 5
  • 14
Shakti S
  • 21
  • 10
  • 2
    I would put a try...catch block in the `ExtractDataFromSharePoint` to see whats going on in there – Ben Aug 11 '17 at 05:57

1 Answers1

1

Since you are not getting to the line Job completed, then it will be helpful to add a try catch clause in your ExtractDataFromSharePoint method:

public void ExtractDataFromSharePoint()
{
   try
   {
     Library.WriteErrorLog("inside task");    
     ArchiveAdministration admin = new ArchiveAdministration();
     admin.ArchiveAuto();
     Library.WriteErrorLog("job completed");
     _timer.Stop();
   }
   catch(Exception ex)
   {
     Library.WriteErrorLog(ex.Message); //or try to log the stacktrace also    
   }    
}

Catch the exception and if you couldn't solve it, paste it here to be able to help you with that

alaa_sayegh
  • 2,141
  • 4
  • 21
  • 37
  • hi @alaa_sayegh , by adding try catch in ExtractDataFromSharePoint() method I am getting the below log details. thanks but i duno how to rectify this... 11/08/2017 12:04:07:Timer elapsed 11/08/2017 12:04:07:inside task 11/08/2017 12:04:17:Cannot open database "Data Processing Suite" requested by the login. The login failed. Login failed for user 'NT AUTHORITY\SYSTEM'. – Shakti S Aug 11 '17 at 06:34
  • ok. give the user NT AUTHORITY\SYSTEM right to access the database. see this: https://stackoverflow.com/questions/2251839/login-failed-for-user-nt-authority-network-service – alaa_sayegh Aug 11 '17 at 06:44
  • hi @alaa_sayegh, i have a problem now, :( my method is working but after the task is completed the service is not running after each one minute like before – Shakti S Aug 11 '17 at 07:14
  • @ShaktiS, the scheduler has nothing to do with the database settings that you did. you need to check if your method finishes in 1 minute, if your service works till the end and reaches the stop point. – alaa_sayegh Aug 11 '17 at 07:18