在我的应用程序中,我有一个基于1秒间隔调用方法的计时器。
在该方法中,我将从表中读取一行,如果该表包含预期记录,那么我将进行一些操作。
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");
}
}
但是当我尝试从该表中读取数据时,有时我得到了异常
"Column StationCode does not belong to the table"
但我只是在一段时间内获得异常,如果我重新启动应用程序它正常工作。过了一段时间,同样的异常即将来临。
我怀疑我的计时器出现了什么问题。如果出现任何问题,请指出。
我的计时器事件:
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);
}
}