简体   繁体   中英

creating file for every 24 hours depends on the timer

I have done like this...for executing backup(backup database Using mysql) function for every 24 hours scheduled at night 10 o clock

and it will check the file creation time(backup.sql) also (ie),if there any file that was not created with in 24 hours it will creates the file .....that was done in backupdatabase function...

but it was not creating the file for every day at night 10 o clock ....

I have used timer for creating file on every day at night 10 o clock ...

and this is my code....

public partial class BackupForm : Form
{
 private static System.Timers.Timer _timer;
 private Int32 _hours = 0;
 private Int32 _runAt = 10;

public BackupForm()
{
}
private void BackupForm_Load(object sender, EventArgs e)
{
  _hours = (24 - (DateTime.Now.Hour + 1)) + _runAt;
  _timer = new Timer {Interval = _hours*60*60*1000};
  _timer.Elapsed += new ElapsedEventHandler(Tick);
  _timer.Start();


}

void Tick(object sender, ElapsedEventArgs e)
{
  string hostname = MainHelper.getServer();
  const string path = @"C:\folder\Access\backupdb\";
  var listfiles = Directory.GetFiles(@"C:\folder\Access\backupdb\", "backup-*.zip").OrderByDescending(File.GetCreationTime).ToList();
  var getfiles = new List<String>();
  var files = listfiles.Select(Path.GetFileName).ToList();
  var dt = DateTime.Now;
  foreach(var file in files)
  {

    var creationtime = File.GetCreationTime(file);
    var diff = DateTime.Now.Subtract(creationtime);
    if(diff.Hours > 24 && diff.Days < 2 && creationtime.Month == dt.Month && creationtime.Year == dt.Year && hostname == "localhost" && _hours == 24)
    {
      backupDatabase();// here, i am doing backup database(creating backup.Zip file) 

    }
    else if (_hours != 24)
    {
      _hours = 24;
    _timer.Interval = _hours * 60 * 60 * 1000;
    }
  }
 }
}

I don't know where i did wrong ...

My aim is every day at given time a file is created(excutuion of backupdatabase function) and in addition to it has to check the file creation time (with in 24 hours)

would any one pls help on this..

any sample code would be very helpful to me for raising an event at particular time include checking 24 hours ......

Many thanks...In advance

In your Tick event it looks like you need to _timer.Stop() at the start of that event and then _timer.Start() at the end to reset the timer.

Edit:

Step 1: in your BackupForm_Load event you can calculate the next occurrence of 10:00 am by doing the following:

var now = DateTime.Now;
var today = now.Date;
var tenAm = today.AddHours(10).AddDays(now.Hour >= 10 ? 1 : 0);

TimeSpan ts = tenAm - now;

var timeInMillisecondsTill10Am = ts.Milliseconds;

Step 2: in your Tick event call _timer.Stop() at the start of the event. Then set the interval to 24 hours hours from now which I've shown below. And finally call _timer.Start() just before the very last line of your Tick event to reset the timer:

var twentyFourHoursFromNow = TimeSpan.FromHours(24).TotalMilliseconds;

Why reinvent the wheel. You can use enterprise job scheduler like this one found at Quartz.net . Let Quartz.net handle the scheduling and firing the job on time. Invest your quality time in developing the business logic.

Below

    void Tick(object sender, ElapsedEventArgs e)
{

add _timer.Stop();

and before the method end (before it's "}") add _timer.Start();

这对于脚本语言而不是Forms应用程序来说似乎是一项任务,但是......如果您使用Microsoft的Reactive Extensions库,则可以真正简化代码。

Observable.Interval(TimeSpan.FromHours(24)).Subscribe(x => DoBackup());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM