本文介绍了从timer调用时,方法返回意外的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 真的,我很沮丧,我不知道出了什么问题。 在我的申请中,我有一个计时器根据1秒间隔调用一个方法。 在那个方法中,我将从表中读取一行,如果该表包含预期记录,那么我会做一些操作。 DataTable dtStationCode = new DataTable() ; string LocalStationCode = ; 尝试 { oDataParamBLL = new DataParametersBLL(); oDataParamBLL.Mode = SELECT_STATIONCODE; oDataParamBLL.LineCode = Global.LineCode; oDataParamBLL.OperationCode = Global.OperationCode; dtStationCode = oServerperformBLL.GetStationCode(oDataParamBLL); if (dtStationCode!= null ) { if (dtStationCode.Rows.Count > 0 ) { foreach (DataRow dr in dtStationCode.Rows) { log.Info( 数据表行值站代码: + dr [ 0 ]。ToString()); } LocalStationCode = dtStationCode.Rows [ 0 ] [ StationCode]的ToString(); dtStationCode = null ; } else { log.Fatal( Assy_Stations_Jig_In_Out_Status表中的站点代码不可用); } } 但是当我尝试从该表读取数据时,有时我得到了一个例外 列 StationCode 不属于 但是我我只获得一段时间的异常,如果我重新启动应用程序它正常工作。过了一段时间同样的异常即将来临。 我怀疑我的计时器出现了什么问题。如果有什么不妥,请告诉我。 我尝试过: timerToMonitorExcel = new System.Timers.Timer(); timerToMonitorExcel.Enabled = true ; timerToMonitorExcel.Start(); log.Info( Timer Started); timerToMonitorExcel.Interval = 1000 ; timerToMonitorExcel.Elapsed + = new System.Timers.ElapsedEventHandler(timerToMonitorExcel_Elapsed); 静态 对象 LocktimerToMonitorExcel_Elapsed = new object (); Dictionary< string,string> dictParams = new Dictionary< string,string>(); void timerToMonitorExcel_Elapsed( object sender,System.Timers.ElapsedEventArgs e) { Monitor.Enter(LocktimerToMonitorExcel_Elapsed); 尝试 { GetJigStatus(); } catch (例外情况) { log.Error( Excel Data上的问题,ex); } 最后 { Monitor.Exit(LocktimerToMonitorExcel_Elapsed); } } 解决方案 Hi,Really,i got frustrated ,I don't know what is going wrong.In my application ,I have one timer which invoke a method based on 1 second interval.In that method ,i will read one row from table,if that table contains expected records,then i will do some operations.DataTable dtStationCode = new DataTable(); string LocalStationCode = ""; try { oDataParamBLL = new DataParametersBLL(); oDataParamBLL.Mode = "SELECT_STATIONCODE"; oDataParamBLL.LineCode = Global.LineCode; oDataParamBLL.OperationCode = Global.OperationCode; dtStationCode = oServerperformBLL.GetStationCode(oDataParamBLL); if (dtStationCode != null) { if (dtStationCode.Rows.Count > 0) { foreach (DataRow dr in dtStationCode.Rows) { log.Info("Data Table Row Value Station Code : "+dr[0].ToString()); } LocalStationCode = dtStationCode.Rows[0]["StationCode"].ToString(); dtStationCode = null; } else { log.Fatal("Station Code not Available on Assy_Stations_Jig_In_Out_Status Table"); } }but when i try to read data from that table ,some times i got an exception"Column StationCode does not belong to the table"but I'm getting the exception some time only,If i restart the application it is working fine. after sometime same exception is coming.I'm suspecting my timer is behaving something wrong.please suggest me if anything wrong.What I have tried:timerToMonitorExcel = new System.Timers.Timer();timerToMonitorExcel.Enabled = true;timerToMonitorExcel.Start();log.Info("Timer Started");timerToMonitorExcel.Interval = 1000;timerToMonitorExcel.Elapsed +=new System.Timers.ElapsedEventHandler(timerToMonitorExcel_Elapsed);static object LocktimerToMonitorExcel_Elapsed = new object(); Dictionary<string, string> dictParams = new Dictionary<string, string>(); void timerToMonitorExcel_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Monitor.Enter(LocktimerToMonitorExcel_Elapsed); try { GetJigStatus(); } catch (Exception ex) { log.Error("Issue on Excel Data ", ex); } finally { Monitor.Exit(LocktimerToMonitorExcel_Elapsed); } } 解决方案 这篇关于从timer调用时,方法返回意外的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 02:15