-1

I am building a service.

This service process and move files from one forlder to another.

There is a timer in this service that check every minute to see if there are files to move and move them

what is happening is when it starts it moves the files but if the number of files is big it takes more than 1 minute which leads to call the same procedure again and get errors

I want the timer to be paused till the function within finishes then back again

here is my code

    protected override void OnStart(string[] args)
    {

            Timer timer = new Timer();
            timer.Interval = IntervalLength;
            timer.Elapsed += new ElapsedEventHandler(this.OnTimer);
            timer.Start();
    }

    public void OnTimer(object sender, ElapsedEventArgs args)
    {
        this.Stop();

        DO();

        this.Start();

    }

    private void DO()
    {
     //Process and move files
    }

I added Stop to stop the timer but Start() is give this error

Severity Code Description Project File Line Suppression State Error CS1061 'XMI2DBSrv' does not contain a definition for 'Start' and no accessible extension method 'Start' accepting a first argument of type 'XMI2DBSrv' could be found (are you missing a using directive or an assembly reference?) XMI2DB E:\XMI2DBsrv.cs 73 Active

how to do that?

asmgx
  • 7,328
  • 15
  • 82
  • 143

1 Answers1

1

In your OnTimer method, you are using this. However this refers to the class that contains the method, rather than the Timer object.

You need to call Start and Stop on the Timer object, which is sender in this case.

Therefore, what you are looking for is ((Timer)sender).Start();.

Loocid
  • 6,112
  • 1
  • 24
  • 42