我有一个 winforms 应用程序,我将通过任务调度程序调用它来下载文件并将它们插入数据库。但是,文件何时可用是不可预测的,因此我使用 timer ( System.Windws.Forms.Timer
) 来轮询文件。
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();
}
}
在上面的代码片段中,OptionsTradedTimer
/OptionsStatsTimer
都轮询文件,然后开始以 . 结尾的进程Application.Exit()
。这运行得很完美,但之后它陷入了无限的消息循环。我认为这Application.Run()
会在第一个计时器滴答后立即被调用,因此当Application.Exit()
最终被调用时,它将结束消息循环。但是,如果我单步执行代码,然后Application.Exit()
程序将返回到它起源的 timer.tick(),然后继续Application.Run()
到底部。底部的Application.Run()
是必要的,因为没有它,计时器只会滴答一次,然后应用程序将退出。
那么如何正确地告诉应用程序退出呢?或者我应该在哪里打电话Application.Run()
?