Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Hidden Program Timer

I've created a C# console application and changed the output type to a Windows Application in Project > Project Properties to create a hidden program.

My main method looks like this:

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
}

The program should be hidden and call the method runProgram every 10 seconds.

When I compile this as a Console Application it works fine. But when I try to compile as a Windows Application it doesn't work. My guess is the timer is not working when compiled as a Windows Application.

How to accomplish this?

like image 702
Hussain Noor Mohamed Avatar asked Jun 30 '26 05:06

Hussain Noor Mohamed


1 Answers

You can't call Console.ReadLine(); without a console.

Instead, you should call either Application.Run() (to run a message loop) or Thread.Sleep(Timeout.Infinite) (to hang forever).

like image 59
SLaks Avatar answered Jul 01 '26 17:07

SLaks