当我用F10按钮一步步调试这个小程序时,程序是有理的,直到达到关卡为止timer.Elapsed +=
。在此之后它应该去调用我的方法Check(MyConn)
,但它没有!它回到MyConn.Close();
并在这两者之间反弹,然后突然关闭程序!
我想知道问题出在哪里......它可能来自这一行:timer.Elapsed += (timerSender, timerEvent) => timer_Elapsed(timerSender, timerEvent, MyConn);
?这是在这个论坛上发布的一个解决方案,以防我想提出MyConn
论点timer_Elapsed
......
在此先感谢您的帮助!
static void Main(string[] args)
{
// create connection
string ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\mike\\Documents\\Database1.mdb;";
OleDbConnection MyConn = new OleDbConnection(ConnStr);
MyConn.Open();
initTimer(MyConn);
MyConn.Close();
}
static void initTimer(OleDbConnection MyConn)
{
//set up a timer
Timer timer = new Timer();
timer.Interval = 2000; // check every 2s (2000ms) if the values in the database changed
timer.Enabled = true; //enable the timer, so when the timer elapses after 2s, it performs some calculations
timer.Elapsed += (timerSender, timerEvent) => timer_Elapsed(timerSender, timerEvent, MyConn);
}
static void timer_Elapsed(object sender, ElapsedEventArgs e, OleDbConnection MyConn)
{
Check(MyConn); // Check is a method I have in my program which takes as argument "MyConn"
}