Click here to Skip to main content
14,983,791 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a c# code as a windows service to logs all log on and log off of all users in a network and save them on a table in server.

I have used a
loop with
condition and check the system current user every 1 minutes in this loop so my service goes to 'Starting' status when i turn on my system.

everything goes fine up to here, but the problem is that the
method does not call when i shut down my system.I know the problem is about the status of the service when it starts because when i comment the While loop in
method the codes in
method will be done.

Maybe the problem is that the OnStart method is not meant for a loop; it's only meant for initializing the service.[^]

The OnStart method must return to the operating system once the service's operation has begun. It must not loop forever or block.
M_Mogharrabi 9-Jul-12 4:32am
Hi KornaKar, Thanks for your immediate reply.I have make a Thread and do my work in it and used System.Timers.Timer in it instead of while loop.but i have my problem yet.I mean the OnStop() method does not start automatically...
Dont add loop in service code. Instead create one another application which has the loop code in it and fron your service's OnStart method, start that application. And from your OnStop method, you kill that application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900