I Create service using C#.I use the coding part of test to create servicee.Once I start the service it gave me message saying

"Myservice on local computer started and then stoped.Some services
stop automaticaly if they have no work to do,for example ,the performance logs and alerts service"

below I mention the coding part

protected override void OnStart(string[] args)

            eventLog1.WriteEntry("Test", EventLogEntryType.Information);
            FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
            m_streamWriter.WriteLine(" mcWindowsService: Service Started \n");

I write the coding for Stop function.Please help me to slove this problem.

I think you need to launch a thread in the OnStart() to keep the service alive. When a Service calls the OnStart() it does not report the service as "running" until the main thread finishes -- and it probably has to have another thread to keep it alive.

Unlike form applications that block the main thread until the application exits, windows will hold on to the main service thread.


I am not good at thread.is there another way to do this.


You can drop a timer in your service and have the timer fire off on an interval


I would like to ask if i intend to copy or capture the windows login name, how am i to go about doing it?

I tried

string Name = Environment.GetEnvironmentVariable("USERNAME");
string FirstPresentTime = DateTime.Now.ToString();

then write it to txt file but the name is blank but the Date&time appeared and if i change it to

string Name = ("USER");

the USER and Date&time will both appear.


I think you should start new thread for such question. but before this search in this forum regarding your problem.