Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows service on Local Computer started and then stopped error

Usually, I get this error: (The "service name" service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other service or programs) when there's something wrong with my code, like non-existing drive paths, etc. The windows service will not start.

I have a windows service that backs up folder/files, to a location if it reached the size limit. Details are all provide by an XML Configuration that the windows service reads on start. I have a separate windows forms that has a button that does exactly what my windows service's onstart is doing. I use my windows forms for debugging the code before I put it in my windows service.

When I start my windows forms. It does what it suppose to do. When I put my code in the windows service OnStart() method the error showed up.

Here's my code:

protected override void OnStart(string[] args) {      private static string backupConfig = @"D:\LogBackupConfig\backupconfig.xml";     private static string serviceStat = @"D:\LogBackupConfig\Status.txt";     private static string fileFolderStat = @"D:\LogBackupConfig\FileFolderStat.txt";      protected override void OnStart(string[] args)     {         if (File.Exists(backupConfig))         {             FileSystemWatcher watcher = new FileSystemWatcher();             XmlTextReader reader = new XmlTextReader(backupConfig);              XmlNodeType type;             List<string> listFile = new List<string>();             string fileWatch = "";              //this loop is for reading XML elements and assigning to variables             while (reader.Read())             {                 type = reader.NodeType;                 if (type == XmlNodeType.Element)                 {                     if (reader.Name == "File")                     {                         reader.Read();                         fileWatch = reader.Value;                     }                     else if (reader.Name == "Folder")                     {                         reader.Read();                         fileWatch = reader.Value;                     }                 }             }             reader.Close();              watcher.Path = fileWatch;             watcher.Filter = "*.*";              //this loop reads whether the service will watch a file/folder             XmlTextReader reader1 = new XmlTextReader(backupConfig);             while (reader1.Read())             {                 type = reader1.NodeType;                 if (type == XmlNodeType.Element)                 {                     if (reader1.Name == "File")                     {                         watcher.IncludeSubdirectories = false;                         watcher.Changed += new FileSystemEventHandler(OnChangedFile);                     }                     else if (reader1.Name == "Folder")                     {                         watcher.IncludeSubdirectories = true;                         watcher.Changed += new FileSystemEventHandler(OnChangedFolder);                     }                 }             }             reader1.Close();              watcher.EnableRaisingEvents = true;          }         else         {             StreamWriter sw = new StreamWriter(serviceStat, true);             sw.WriteLine("File not found. Please start the Log Backup UI first.");             sw.Close();         }     } 

I don't know what keeps the windows service not starting, the windows form simulator worked fine. What seems to be the problem?

UPDATE: After many trials I've noticed that using only a folder directory (w/out file), the windows service doesn't work. When I replaced the fileWatch variable with a specific file (including its directory), the windows service started. When I changed it back to a folder location, it didn't work. What I think is that folder locations doesn't work in a filewatcher.

When I tried creating a new windows service that watches a folder location, it worked.. However, when I tried the same location in my original windows service, it didn't work! I was mindf$#*ed! It seems that I have to create a new windows service and build the installer everytime I place a new code/function.. This way I can keep track where I get an error.

like image 606
Blackator Avatar asked Aug 31 '12 04:08

Blackator


People also ask

How do you fix the Task Scheduler service on local computer started and then stopped?

Type Services. msc in Start Search, open the app, scroll down to Task Scheduler service, click on it, make sure it is Running and Set to Automatic. Then click the Dependencies tab, make sure those services are running too.

Why services are getting stopped automatically?

Some Services stop automatically if they are not in use by another services or programs. What's the problem and what's the solution? this error would occur with a Java service when the Java policy doesn't allow proper permissions, such as "policy.

How do you fix some services stop automatically if they are not in use by other services or programs?

You can fix this by going to the list of services, right click on desired service, click properties on context menu and then change "Log On As" to "Local system account" if it is not the same as shown in below image. Show activity on this post.

What does the service on local computer started and then stopped mean?

“The Service on local computer started and then stopped ,Some services stop automatically if there are not in use by other services or programs.” Now I will explain how to solve the Service on local computer started and then stopped, some services stop automatically if there are not in use by other services or programs.

How to fix local system account not recognized in Windows 10?

First Way Start --> Run --> Type Services.msc and click enter button now you will get all the services in your computer after that select your service and right click on that and go to Properties After that open Select Log On tab in that select Local System Account and click ok now your problem will solve Otherwise check second way.

Why do some services stop automatically?

Some services stop automatically if they are not in use by other services or programs" When I went to look at my tnsnames.ora and listener.ora files, they were not present in the ADMIN folder.

Why does Windows Defender Network Inspection Service stop automatically?

Some services stop automatically if they are not in use by services or programs. This started after today's update 8/7/2015. I was able to start Windows Defender Network Inspection Service but it says startup type is Manual. That doesn't seem right. This thread is locked.


2 Answers

If the service starts and stops like that, it means your code is throwing an unhandled exception. This is pretty difficult to debug, but there are a few options.

  1. Consult the Windows Event Viewer. Normally you can get to this by going to the computer/server manager, then clicking Event Viewer -> Windows Logs -> Application. You can see what threw the exception here, which may help, but you don't get the stack trace.
  2. Extract your program logic into a library class project. Now create two different versions of the program: a console app (for debugging), and the windows service. (This is a bit of initial effort, but saves a lot of angst in the long run.)
  3. Add more try/catch blocks and logging to the app to get a better picture of what's going on.
like image 95
McGarnagle Avatar answered Oct 12 '22 23:10

McGarnagle


Not sure this will be helpful, but for debugging a service you could always use the following in the OnStart method:

protected override void OnStart(string[] args) {      System.Diagnostics.Debugger.Launch();      ... } 

than you could attach your visual studio to the process and have better debug abilities.

hope this was helpful, good luck

like image 40
Eyal H Avatar answered Oct 12 '22 23:10

Eyal H