日期:2014-05-16  浏览次数:20834 次

用定时器触发事件时会使程序未响应(小虾米求助)
我用System.Timers;设置一个定时1s的定时器,时间到了触发事件,现在每次触发都会使程序未响应,不知道怎么解决。我觉得在waitTime中是把定时器初始化,事件结束时要释放定时器,但我不知道怎么写。所以是不是因为没释放定时器引起的。求助。。。
以下是部分代码
 private void waitTime()
       {
           System.Timers.Timer time = new System.Timers.Timer();
           time.Interval = 1000;
           time.AutoReset = false;
           time.Start();
           time.Elapsed += new ElapsedEventHandler(time_Elapsed);
       }

       private void time_Elapsed(object sender, ElapsedEventArgs e)
       {
           string selectSql1 = "SELECT * FROM 物主表 WHERE 学号=" + ID;
           string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:\\DB_Student.accdb";
           string wuzhuTime = "";
           OleDbConnection connection = new OleDbConnection(connectionString);
           OleDbCommand command = new OleDbCommand(selectSql1, connection);
           connection.Open();
           OleDbDataReader reader = command.ExecuteReader();

           while (reader.Read())
           {            
               wuzhuTime = reader.GetString(4);
           }
           reader.Close();

           connection.Close();
           
           if (TimeSubtract(wuzhuTime, wupinTime) == false) // 有情况
           {
               warnForm f1 = new warnForm();
               f1.Show();
           }
       }
------解决方案--------------------
time.enable=true;
------解决方案--------------------
用System.Windows.Forms中的Timer
------解决方案--------------------
估计设置一个定时1s的定时器,这个时间太短了,你读取数据库来不及响应