Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using console application.
protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            DateTime dt = DateTime.Now;
            
            string filePath = string.Format ("{0}{1}.txt" ,"D:/swathi/","startFile");
            StreamWriter sw = new StreamWriter(filePath);
            sw.Write("service opened at");
            sw.WriteLine(dt);
            sw.Close();
            
           
        }
        protected override void OnStop()
        {
            DateTime dt = DateTime.Now;
            string filePath = string.Format("{0}{1}.txt", "D:/swathi/", "closeFile");
            StreamWriter sw = new StreamWriter(filePath);
            sw.Write("Service closed at");
            sw.WriteLine(dt);
            sw.Close();
            base.OnStop();
        }


When does Onstart method gets called?
What is the use of services?
Posted

Start with this [^]link...

OnStart[^]
 
Share this answer
 
Services are long-running executables that run without a user interface, often used to implement server software.

OnStart executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically).

OnStop executes when a Stop command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service stops running.

Regards
Espen Harlinn
 
Share this answer
 
Well, I would suggest you to have a read at these:
Simple Windows Service Sample[^]
Introduction to Windows Service Applications [^]
 
Share this answer
 
I'd also like to point out that unless you have very specific reasons for doing so, avoid writing files anywhere but the appropriate special folder.
 
Share this answer
 
Windows Services can be set to start automatically when the computer reboots by setting the StartType on the service installer to Automatic. In such a situation, OnStart would be called at system startup.If it's set to manual,as soon as you start it,OnStart fires.
OnStop Specifies actions to take when a service stops running.

As an example for what is the use of Windows Service,consider this :Say you have a scenario where files needs to be copied from one server to another server through a wcf service based on various conditions.Here you can implement the addfiletoshare/removefilefromshare functionalities in a windows service & can be invoked from the wcf service.

Also,I would suggest instead of writing to a file when the service got started or stopped the right way is to write it to the event log or to the database.You can use Log4Net for your logging purposes.

Hope you got the point!
 
Share this answer
 
v2

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