3 Replies - 762 Views - Last Post: 06 February 2013 - 05:14 AM Rate Topic: -----

#1 small_chick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-July 12

Keep the timer alive until the end of Main ?

Posted 06 February 2013 - 03:09 AM

i've got the code below:
using System;
using System.Timers;

public class Timer1
{
    public static void Main()
    {
        // Normally, the timer is declared at the class level, so
        // that it doesn't go out of scope when the method ends.
        // In this example, the timer is needed only while Main 
        // is executing. However, KeepAlive must be used at the
        // end of Main, to prevent the JIT compiler from allowing 
        // aggressive garbage collection to occur before Main 
        // ends.
        System.Timers.Timer aTimer = new System.Timers.Timer();

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        // Set the Interval to 2 seconds (2000 milliseconds).
        aTimer.Interval = 2000;
        aTimer.Enabled = true;
 
        Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();

        // Keep the timer alive until the end of Main.
        GC.KeepAlive(aTimer);
    }
 
    // Specify what you want to happen when the Elapsed event is 
    // raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Hello World!");
    }
}


The purpose of the instruction "GC.KeepAlive(aTimer);" is to keep the timer does not count until reaching the end of Main( ).Is this true ? ^~^

Is This A Good Question/Topic? 0
  • +

Replies To: Keep the timer alive until the end of Main ?

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5513
  • View blog
  • Posts: 11,820
  • Joined: 02-June 10

Re: Keep the timer alive until the end of Main ?

Posted 06 February 2013 - 04:52 AM

This makes no sense - much like your question. No idea what you are asking.

However line 28 is already at the end of your Main() - so what do you think is going to be any different now that you've added this line?

You wrote it - you have the project on your computer - you tell us... What do you observe it doing?
Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Keep the timer alive until the end of Main ?

Posted 06 February 2013 - 04:56 AM

He just copied the example from MSDN
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5513
  • View blog
  • Posts: 11,820
  • Joined: 02-June 10

Re: Keep the timer alive until the end of Main ?

Posted 06 February 2013 - 05:14 AM

Not really - the example *has* an explanation in the code for what that line does.

So the OP copied the code - then took out the explanation - the asked us what it did. <sarcasm>That's helpful and effective</sarcasm>


Quote

// If the timer is declared in a long-running method, use 
        // KeepAlive to prevent garbage collection from occurring 
        // before the method ends. 
        //GC.KeepAlive(aTimer);



Quote

The purpose of the instruction "GC.KeepAlive(aTimer);" is to keep the timer does not count until reaching the end of Main( ).Is this true ? ^~^


NO - As the comments say - it is to keep Garbage Collection from happening before the method has completed.

GC = Garbage Collection
GC.KeepAlive = Keep the Garbage Collection alive
GC.KeepAlive(aTimer) = Keep the Garbage Collection alive based on aTimer

English is obviously not your native language. But you are choosing to use English websites for reference and questions. This is bound to be a problem.

You should probably look into good translation websites - coding websites in your native language - or make friends at school with someone who speaks English and can translate for you.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1