Ciao Alessandro,
il tuo caso è molto semplice, un qualsiasi programma conta sempre di un entry-point, cioè un metodo che viene eseguito come funzione main dell'applicazione, e nella maggior parte dei casi se un metodo main si conclude, anche l'applicazione/processo/domain ad esso connesso si chiude, nel tuo caso appunto la console ha la sua funzione main, e per tenere in vita eventuali thread in esecuzione, devi per forza tenere in vita il processo che li hosta e di conseguenza la tua console application, per fare questo in una console app di solito si rimane in attesa di un input da parte dell'utente prima di chiudere l'applicazione:
Console.WriteLine("Premi ENTER per chiudere l'applicazione...");
Console.ReadLine();
vorrei aggiungere solo una cosa che magari non sai, un System.Timers.Timer (http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.100).aspx) di default già utilizza uno o più thread quando esegue la callback Elapsed (If the SynchronizingObject property is null, the Elapsed event is raised on a ThreadPool thread.), quindi un timer startato nella main della console app non bloccherà mai l'esecuzione del programma. Se apri il link msdn vedrai che c'è proprio un esempio al tuo caso, crea un timer, lo inizializza, lo avvia e subito dopo mette la console in attesa di input da parte dell'utente, proprio per non fare chiudere subito l'applicazione.
using System;
using System.Timers;
public class Timer1
{
private static System.Timers.Timer aTimer;
public static void Main()
{
// Normally, the timer is declared at the class level,
// so that it stays in scope as long as it is needed.
// If the timer is declared in a long-running method,
// KeepAlive must be used to prevent the JIT compiler
// from allowing aggressive garbage collection to occur
// before the method ends. You can experiment with this
// by commenting out the class-level declaration and
// uncommenting the declaration below; then uncomment
// the GC.KeepAlive(aTimer) at the end of the method.
//System.Timers.Timer aTimer;
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// 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();
// 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);
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
Ciao!
----------------------------------------------
Michael Denny
Lead Software Developer & Solutions Architect
Microsoft Framework (C# ASP.NET VB.NET)
http://blogs.dotnethell.it/Regulator/