首页 新闻 搜索 专区 学院

winform 做个托盘工具,每天定时扫描指定文件是否有备份,文件无更新则发送邮件?求代码,要求用timmer

0
悬赏园豆:5 [待解决问题]

我的思路是这样:写一个发邮件的方法,然后用timmer,当时间等于23::5:59时,调用发邮件方法?问题来了,时间间隔怎么设置?间隔二十四小时扫描一次?时间条件判断写在哪里呢?我把判断条件写在发送邮件的方法里,好像变成不断发重复邮件了,这是涉及线程问题吗?本人菜鸟,求指点~求demo

问题补充:

代码补充,求大虾帮忙看看  

namespace widFileCheck
{
    public partial class Form1 : Form
    {
        System.Timers.Timer t = new System.Timers.Timer(86400000);//24小时执行一次
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Process
            System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
            System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);
            //查找相同名称的进程
            foreach (System.Diagnostics.Process process in processes)
            {
                //忽略当前进程
                if (process.Id != current.Id)
                {
                    //确认相同进程的程序运行位置是否一样.
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        //Return the other process instance.
                        Application.Exit();
                    }
                }
            }
            t.Start();
            Application.DoEvents();
            t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;
            t.AutoReset = true;//配置是执行一次(false)还是一直执行(true);
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
            t.Start();
        }
        public void theout(object source, System.Timers.ElapsedEventArgs e)
        {
            DateTime dateTimeNow = DateTime.Now;
            int nowHour = dateTimeNow.Hour;
            int nowMinute = dateTimeNow.Minute;
            int nowMillisecond = dateTimeNow.Millisecond;
            int checkHour = 23;
            int checkMinute = 59;
            int checkMillisecond = 59;
            #region 扫描文件
            IPAddress[] HostAdress = Dns.GetHostByName(Dns.GetHostName()).AddressList;//获得当前电脑IP地址
            string HostIpAdress = HostAdress[0].ToString();
            string strDateTimeNow = dateTimeNow.ToShortDateString();//当前时间
            string checkDateSql = "select * from T_CheckBack where f_ipaddress='" + HostIpAdress + "'and f_nextDate='" + strDateTimeNow + "'" + "and F_FileSate='0'";
            DataTable dt = SqlHelper.ExcutDaTaAdaper(checkDateSql);
            if (dt.Rows.Count > 0)
            {
                if (nowHour == checkHour && nowMinute == checkMinute && nowMillisecond == checkMillisecond)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ArrayList noBackupFile = new ArrayList();//未备份文件
                        //string[] noBackupFile = new string[] { };//未备份文件
                        DataRow dr = dt.Rows[i];
                        string fileName = Path.GetFileName(dr[2].ToString()).ToString();//文件名
                        FileInfo info = new FileInfo(fileName);
                        DateTime lastUpdateTime = info.LastWriteTime;//最后更新时间
                        if (lastUpdateTime.ToShortDateString() != strDateTimeNow)
                        {
                            ArrayList mailAdress = new ArrayList();//收件人
                            mailAdress.Add("");
                            mailAdress.Add("");
                            string fromEmail = "";
                            string fromName = "";
                            string reEmail = "";
                            //string toEmail = "";
                            string mailTitle = "服务器未及时备份文件提醒";
                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("各位:");
                            sb.AppendLine("  " + HostAdress + "服务器文件:" + fileName + "未及时备份,请注意,谢谢。");
                            string mailBody = sb.ToString();
                            string filenames = fileName;
                            noBackupFile.Add(fileName);//文件
                            MailClass mail = new MailClass();
                            for (int j = 0; j < mailAdress.Count; j++)//发送邮件
                            {
                                mail.SendMailnew(fromEmail, fromName, reEmail, mailAdress[j].ToString(), mailTitle, mailBody, filenames);
                            }
                            string NextCheckDate = Convert.ToDateTime(dr[4]).AddDays(Convert.ToDouble(dr[3])).ToString();
                            string fineseSql = "update T_CheckBack set F_CheckDate='" + strDateTimeNow + "',F_FileSate='1'";
                            int resul = SqlHelper.ExceutNoneQuerry(fineseSql);//更新检查标记
                            if (resul < 0)
                            {
                                MessageBox.Show("系统出错,请联系相关人员");
                                return;
                            }
                        }
                        else
                        {
                            string insertNewSql = "insert into T_CheckBack(F_IPAddress,F_FileName,F_CheckPeriod,F_NextDate) values(' ";
                            insertNewSql += dr[1] + "','" + dr[2] + "','" + dr[3] + "','" + dr[4] + "'";
                            int re = SqlHelper.ExceutNoneQuerry(insertNewSql);
                            if (re < 0)
                            {
                                MessageBox.Show("系统出错,请联系相关人员");
                                return;
                            }
                        }
                    }
                }

            }
            #endregion
        }

87ZONE的主页 87ZONE | 初学一级 | 园豆:199
提问于:2015-01-18 20:36
< >
分享
所有回答(4)
0

.......................

定时任务

吴瑞祥 | 园豆:28820 (高人七级) | 2015-01-18 20:52

因为一些实际情况,就是指明不能用计划任务阿

支持(0) 反对(0) 87ZONE | 园豆:199 (初学一级) | 2015-01-19 09:16

@87ZONE: quartz.net不考虑?

支持(0) 反对(0) 吴瑞祥 | 园豆:28820 (高人七级) | 2015-01-19 09:30
0

写一个程序,功能是扫描文件并根据结果发邮件,然后新建一个计划任务,定时调用这个程序就行了。。。

用计划任务比自己写timer更可靠。

XiaoFaye | 园豆:3082 (老鸟四级) | 2015-01-19 09:12

因为一些实际情况,就是指明不能用计划任务阿

支持(0) 反对(0) 87ZONE | 园豆:199 (初学一级) | 2015-01-19 09:16
0

时间间隔缩小成1000,另外加个标记把多线程处理成单线程就可以了.

87ZONE | 园豆:199 (初学一级) | 2015-01-19 11:03
0

1.单线程可以解决,也可以另外加个线程控制定时器的休眠(不必要执行的时间段让定时器休眠)

2.既然是发邮件(本身就会有延迟),时间的误差性就可以放宽点,定时半分钟,1分钟执行一次都可以(看你实际情况),既然要检查时间 23::5:59(这个是什么格式?)你的定时24小时肯定是不行的啦。 检查时间的方法独立开来 bool CheckTime()

定时做得事情就只要 CheckTime,(....Elapsed +=CheckTime())。 根据CheckTime的返回值 在来执行你的扫描备份和是否发送邮件。

AshEs丶 | 园豆:251 (菜鸟二级) | 2015-01-19 11:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册