Hi

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");
            m_streamWriter.Flush();
            m_streamWriter.Close();
        }

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.

Hi

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

Tank50

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

Hi,

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.

Thanks,
keat

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.