C# 隐藏程序计时器

3

我创建了一个C#控制台应用程序,并在 项目 > 项目属性 中将输出类型更改为Windows应用程序,以创建一个隐藏的程序。

我的主要方法看起来像这样:

static void Main(string[] args)
{
    System.Timers.Timer timer = new System.Timers.Timer(); // Initialize a timer
    timer.Elapsed += new System.Timers.ElapsedEventHandler(runProgram); // to call method runProgram
    timer.Interval = 10000; // every 10 seconds
    timer.AutoReset = true; // which auto-resets
    timer.Enabled = true; // Enable timer
    timer.Start(); // Start timer
    Console.ReadLine(); // Prevent program from terminating
}

程序应该是隐藏的,并且每10秒调用一次runProgram方法。当我将其编译为控制台应用程序时,它可以正常工作。但是,当我尝试将其编译为Windows应用程序时,它就不起作用了。我的猜测是当编译为Windows应用程序时定时器不起作用。如何实现这个目标?

编译成 Windows 应用程序是什么意思? - SpaceSteak
"不起作用" 是什么意思? - SLaks
@SpaceSteak 项目属性 > 应用程序 > 输出类型:改为“Windows 应用程序”? - Hussain Noor Mohamed
1个回答

1

如果没有控制台,您无法调用 Console.ReadLine();

相反,您应该调用Application.Run()(运行消息循环)或Thread.Sleep(Timeout.Infinite)(永久挂起)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接