Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here is my problem ....

I am doing backup schedule every day (i.e) if any backup file is not created with in 24 hours
,I want to create new backup file(`executing backupdatabase() function`) .....

In addition to , I have used timer , `so that every hour it will be checked whether the file is created` or not....and after 24 hours it will created new file (`executing backupdatabase() function`)

File checking is done by using File.GetcreationTime....whether the file is created or not..

For that i have done like this...



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

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

 }

 void Tick(object sender, ElapsedEventArgs e)
 {
   _timer.Stop();
   var name = "localhost";
   const string path = @"C:\Creation\Name\backupdb\";
   var listfiles = Directory.GetFiles(@"C:\Creation\Name\backupdb\", "backup-*.zip").ToList();
   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 && name == "localhost" && _hours == 24)
     {
       backupDatabase();

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




But it does not working , it does not checking the file creation time and even it does not creating the file after 24 hours...

how to check the file with creation time along with timer for every one hour and after 24 hours how to create the file ....

any help that would very helpful..
Posted

Try like this..,

C#
void Tick(object sender, ElapsedEventArgs e)
 {
   _timer.Stop(); _timer.Enable = false;
   var name = "localhost";
   const string path = @"C:\Creation\Name\backupdb\";
   var listfiles = Directory.GetFiles(@"C:\Creation\Name\backupdb\", "backup-*.zip").ToList();
   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 && name == "localhost" && _hours == 24)
     {
       backupDatabase();

     }
     else if (_hours != 24)
     {
       _hours = 24;
       _timer.Interval = _hours * 60 * 60 * 1000;
     }
   }
   _timer.Start(); _timer.Enable = true;
  }
 }
 
Share this answer
 
Comments
kumar0425 8-Nov-11 7:38am    
it does not enter into this tick function.... _timer.Elapsed += new ElapsedEventHandler(Tick);
_hours = (24 - (DateTime.Now.Hour + 1)) + RUN_AT;

This will set the timer to trip at 10h the next day, even if it's currently before 10. It also means that the first time the timer trips (the next morning), the if check will fail (_hours != 24) so nothing will happen. You need to wait until the second day for anything to happen.

&& creationtime.Month == dt.Month && creationtime.Year == dt.Year &&

What is the purpose of this check? Checking diff.Days will already only return recent files. This just means that on the first day of the month your code won't work.
 
Share this answer
 
Comments
kumar0425 8-Nov-11 7:33am    
so what i have to do if i have do like this.. i am checking every one hour upto 24 hours, that the file is created or not after 24 hours i want to execute the backupdatabase() function would you pls give any solution .
mistake is here,

C#
_timer = new Timer { Interval = _hours * 60 * 60 * 1000 };


to run the timer at every one hour interval you dont need to set those many things just set it to fixed 3600000 that is 1 hour, ( 1 (hour) * 60 (Minutes) * 60 (Seconds) * 1000 (MS)).

while testing purpose set it to 2-5 minutes, and test it.
 
Share this answer
 
Comments
kumar0425 8-Nov-11 7:31am    
i am checking every one hour upto 24 hours, that the file is created or not after 24 hours i want to execute the backupdatabase() function would you pls give any solution ..
First, why are you creating a winform app? This should be a windows service so that it runs even when there is no user logged onto the system.

Second, you should use a thread to perform the task, or even a BackgroundWorker object.
C#
//-----------------------------------------------------------------------------
private void BackupScheduleThread()
{
    Thread backupThread = null;
    DateTime now        = DateTime.Now;
    DateTime nextBackup = new DateTime(now.Year, now.Month, now.Day, 0, 1, 0, DateTimeKind.Local);
    if (now > nextBackup)
    {
        nextBackup = nextBackup.AddDays(1);
    }
    do
    {
        // replace this test with my tip/trick describing partial date/time equality
        if (now == nextBackup)
        {
            nextBackup.AddDays(1);
            backupThread = new Thread(new ThreadStart(BackupFilesThread));
            backupThread.Start();
        }
        now = DateTime.Now;
	Thread.Sleep(1000);
    } while (true);
}

//-----------------------------------------------------------------------------
private void BackupFilesThread()
{
    // In this method, all you have to do is concentrate on making your 
    // backup. There is no need to worry about the date/time - just backup 
    // your data because you know it's time to do it (otherwise, this code 
    // wouldn't even be executing).
}

To start the ball rolling, just do this:
C#
Thread scheduleThread = new Thread(new ThreadStart(BackupScheduleThread));
scheduleThread.Start();

I referenced a tip/trick in the code above regarding my DateTime extension method that allows you to compare any part of a two dateTime objects for equality. This tip/trick removes the need to make sure ALL of the fiels are the same when they don't actually matter, such as the seconds and milliseconds. You can get the code here:

Partial DateTime Object Equality[^]

Once you've implemented this, you will have isolated the scheduling from the act of creating the file.
 
Share this answer
 
v5
Comments
kumar0425 8-Nov-11 8:16am    
Many thanks.. but how can i create window service would you explain if possible with steps ... that would be very helpful to me....
#realJSOP 8-Nov-11 8:39am    
That's a different question. Despite the current world economic crisis going on, google is still free.
kumar0425 8-Nov-11 8:20am    
can i write that code in form....same as like method in form.....
or do i have to create window service..and attach to my project...
kumar0425 8-Nov-11 8:24am    
how can i replace the partial datetime object equality ...at this
// replace this test with my tip/trick describing partial date/time equality
if (now == nextBackup)
{
nextBackup.AddDays(1);
backupThread = new Thread(new ThreadStart(BackupFilesThread));
backupThread.Start();
}

if possible would you pls explain clearly ....
#realJSOP 8-Nov-11 8:41am    
I don't have the time to write the actual code for you. You're going to have to gather your mad skillz as a developer and muscle through it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900