计时器不适用于使用C#创建文件 [英] timer is not working for file creation using c#

查看:74
本文介绍了计时器不适用于使用C#创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题....

如果每天24小时都未创建任何备份文件,我每天都会做备份计划.
,我想创建新的备份文件(执行backupdatabase()函数").....

此外,我还使用了timer,以便每隔一小时检查一次是否创建文件...,并在24小时后创建新文件(执行backupdatabase()函数).

通过使用File.GetcreationTime .....是否创建文件来进行文件检查..

为此,我做了这样的事情...



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...



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();
  }
 }





但是它不起作用,它不会检查文件创建时间,甚至在24小时后也不会创建文件...

如何使用创建时间以及每隔一小时的计时器检查文件,以及每24小时后如何检查文件....

任何有帮助的帮助..





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..

推荐答案

尝试这样..,

Try like this..,

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;
  }
 }


_hours = (24 - (DateTime.Now.Hour + 1)) + RUN_AT;


这会将计时器设置为在第二天的10h跳闸,即使当前是在10点之前.这也意味着计时器第一次跳闸(第二天早上)时,if检查将失败(_hours!= 24)所以什么都不会发生.您需要等到第二天才能发生任何事情.


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 &&


此检查的目的是什么?检查diff.Days将仅返回最近的文件.这只是意味着您的代码在每月的第一天将无法正常工作.


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.


错误就在这里,

mistake is here,

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



要每隔一小时运行一次计时器,您无需设置很多东西,只需将其设置为固定的3600000,即1小时,(1(小时)* 60(分钟)* 60(秒)* 1000(毫秒)).

在测试目的时,将其设置为2-5分钟,然后进行测试.



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.


这篇关于计时器不适用于使用C#创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆