-1

I have written a method to download some files from the internet(in my c#,mvc web project.). the same file should download each day between a specific time (because it updates).it works fine in the test environment. my problem is if the system is in live is it work properly the same way in test environment. just to show, this is the method

public void setupTimer(TimeSpan savingTime)
        {

            DateTime current = DateTime.Now;
            TimeSpan timeTogo = savingTime - current.TimeOfDay;

            if (timeTogo < TimeSpan.Zero)
            {
                return;
            }

            this.timer = new Timer(x =>
            {
                this.saveXmlFile();
            }, null, timeTogo, InfiniteTimeSpan);

        }
tereško
  • 58,060
  • 25
  • 98
  • 150
bill
  • 854
  • 3
  • 17
  • 41

1 Answers1

1

Maybe (whether something works on a particular environment depends entirely on the environment - we can't possibly say).

If something kills off your process, or an app pool is configured to recycle which changes a particular path of execution, or there's an exception etc. etc. your timer will be disposed of. You don't want that.

As an aside, consider a scheduled task. Depending on how much control you have over the server you may be able to factor this in to a job that runs, and allow Windows to manage the schedule rather than through the use of timers. There are multiple benefits to this.

In a nutshell - your steps to doing this would be:

  1. Extract out the relevant parts of your code (the parts that are to run regularly and all dependencies) into a C# console app
  2. Configure the Windows Task Scheduler to run this console app at a particular time
  3. Test it!

http://windows.microsoft.com/en-gb/windows/schedule-task#1TC=windows-7