Tony
Tony

Reputation: 10208

Passive waiting in c# application

I am developing a c# console application (which will be converted to a windows service later).

My application starts a lot of timers that execute some code in intervals (once a day, once an hour, etc).

I dont want my console application to exit after setting all the timers.

Right now I am using a while true sentence but I think that it is not the correct way.

Any help?

Upvotes: 0

Views: 687

Answers (5)

sloth
sloth

Reputation: 101162

Don't worry about your while loop.

If you're converting your application to a windows service later anyway, then I would not bother with this. When using a service, you would set up your timers in the start method of the service, and the serivce runs until you stop it. So there's no need for a custom application loop.

Upvotes: 1

Chris Dworetzky
Chris Dworetzky

Reputation: 950

Process.GetCurrentProcess().WaitForExit() 

will keep everything running until you exit programatically.

Upvotes: 0

Casperah
Casperah

Reputation: 4564

When I develop windows services I usually create a module (dll) that contains the code that handles all the logic. Then I create a windows form that calls Start Service and Stop service for development and a Service project for deployment.

Upvotes: 3

eyossi
eyossi

Reputation: 4340

You should use Console.ReadKey() after you have set the times while you are using a console application...

when you will convert it to a service you you won't have to use this line anymore because you will implement the timers initialization on "service start" and it will stay up and running when the code block is over

Upvotes: 1

Brian
Brian

Reputation: 2239

at the end of the code you can add

Console.WriteLine("Press any key to close"); 
Console.ReadKey();

or

Console.ReadLine();

either of these will stop the console from closing until you press a key.

Upvotes: 4

Related Questions