I have a winforms application that I will be calling through the task scheduler to download files and insert them to a database. However it is unpredictable when the files are available so I have used timer (System.Windws.Forms.Timer
) to poll for the files.
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args != null && args.Length > 0)
{
Form1 f1 = new Form1();
if (f1.IsLive)
{
f1.OnLaunch(); // tests DB connection and reads and updates the expiry date list.
if (args[0] == "FullStats")
f1.FullStatsDataPump();
else if (args[0] == "OptionsStats")
{
f1.OptionsStatsTimer_Tick(null, null);
f1.OptionsStatsTimer.Start();
}
else if (args[0] == "OptionsTraded")
{
f1._optionsTradedTimer_Tick(null, null);
f1.OptionsTradedTimer.Start();
}
else
{
EmailManager.InvalidInputArgument(args[0]);
Application.Exit();
}
Application.Run();
}
}
In the above code snippet, OptionsTradedTimer
/ OptionsStatsTimer
both poll for files and then begin processes that end with Application.Exit()
. This runs perfectly but then afterwards it gets stuck in an infinite message loop. I thought that Application.Run()
would be called immediately after the first timer ticks and thus when Application.Exit()
eventually gets called it would end the message loop. But if I step through the code then after that Application.Exit()
the program returns to the timer.tick() where it originated and then continues on to the Application.Run()
at the bottom. The Application.Run()
at the bottom is necessary as without it the timers will only tick once and then the app will exit.
So how do I correctly tell the application to exit? Or where should I be calling Application.Run()
?