Mike
Mike

Reputation: 25

Methods and Timer in C#

When I debug this small program step by step with F10 button, the program is rational until it reaches the level timer.Elapsed +=. after this it is supposed to go call my method Check(MyConn) but it doesn't! it goes back to MyConn.Close(); and it bounces between these two, then it closes the program as of a sudden!

I was wondering where does the issue comes from ... could it be from this line: timer.Elapsed += (timerSender, timerEvent) => timer_Elapsed(timerSender, timerEvent, MyConn);? This was a solution posted on this forum in case I wanted to give in argument MyConn to timer_Elapsed ...

Thanks in advance for your help!

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"
}

Upvotes: 0

Views: 388

Answers (2)

Martin Mulder
Martin Mulder

Reputation: 12954

After you set the timer, your code will just continue running. It will leave the initTimer method and then close the connection. After that, your Main function will exit causing your application to close. Also your timer will terminate then.

In case your program is not being terminated, the timer will be counting down from 2000. When it reaches zero, it will fire the event. This happens on another thread and happens parallel to your main theat. Your even will check the connection, which will obiously be closed.

If you want the main threat to wait till the event is fired, why use the timer at all? Why not do this?

static void Main(string[] args)
{
    // create connection
    string ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\mike\\Documents\\Database1.mdb;";

    using(OleDbConnection MyConn = new OleDbConnection(ConnStr))
    {
        MyConn.Open();
        do
        {
            Thread.Sleep(2000); 
            Check(MyConn);  
        }
        while(someValueToIndicateTheApplicationCanTerminate);
    }
}

Upvotes: 2

musefan
musefan

Reputation: 48415

This is perfectly normal behaviour.

The timer.Elapsed += code is only registering an event. After 2 seconds the event will be fired, but your main thread will continue as normal and return back to the Main() function. It will then call MyConn.Close() and then end the Main() function.

If this is a console application then it will be the end of the program, which means there is no time for the timer_Elapsed event to run.

If it is an application that will "stay alive", like a service or a forms application, then by the time the timer_Elapsed event does fire, the connection will have already been closed anyway.


I would suggest if you do have a "stay alive" app, then you should move the Open/Close connection code to the function that actually makes use of them. Which in this case would be inside the timer_Elapsed event.

If you don't have a "stay alive" app then you probably should avoid timers (and anything with separate threads) altogether.

By the way, I keep saying "stay alive" I hope you know what I mean by that. I don't know a more technical term to describe it.

Upvotes: 3

Related Questions