이 작은 프로그램을 단계별로 F10 단추로 디버깅 할 때 프로그램은 timer.Elapsed +=
수준에 도달 할 때까지 합리적입니다. 이 후 내 방법 Check(MyConn)
을 호출해야하지만 그것은하지 않습니다! 그것은 MyConn.Close();
으로 돌아가고이 둘 사이에서 튀어 오릅니다, 그리고 갑자기 프로그램이 닫힙니다!C에서 메서드 및 타이머
어디에서 문제가 발생했는지 궁금합니다.이 줄에서 올 수 있을까요? 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"
}
몇 가지 - 예외는 무엇입니까? 'Check'는'MyConn' 객체와 무엇을합니까? 왜냐하면 당신은 타이머를 사용할 기회가 있기 전에 즉시 연결을 닫을 것이기 때문입니다. – James