Loko
Loko

Reputation: 6679

C# Windows Service won't start

When I try to start my c# service it says:"starting" for a second and it turns back to being "stopped" What can be the problem? I had the same code before, and it worked but made some changes in the code now and it stopped working. Here is what I added to my code:

App Config:

<add key="cut-copy" value="copy"/>

Normal code:

    private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
    String cut_copy = ConfigurationManager.AppSettings[@"cut-copy"];

if (cut_copy == "copy")
    {
        cut = false;
    }
    else
    {
        cut = true;
    }
if (WaitForFileAvailable(e.FullPath, TimeSpan.FromSeconds(10)))
            {
            var file = Path.Combine(source, e.Name);
            var copy_file = Path.Combine(target, e.Name);
            var destination = Path.Combine(target, Path.ChangeExtension(source, Path.GetExtension(source)));

                    if (File.Exists(file))// Check to see if the file exists. 
                    {                     //If it does delete the file in the target and copy the one from the source to the target.
                        File.Delete(copy_file);
                        File.Copy(e.FullPath, Path.Combine(target, e.Name));
                    }
                    else// If it doesn't, just copy the file.
                    {
                        if (cut == true)
                        {
                            if (File.Exists(file))// Check to see if the file exists. 
                            {                     //If it does delete the file in the target and copy the one from the source to the target.
                                File.Delete(copy_file);
                                File.Move(Path.Combine(e.FullPath, e.Name), target);
                            }
                        }
                        else
                        {
                            if (File.Exists(file))// Check to see if the file exists. 
                            {                     //If it does delete the file in the target and copy the one from the source to the target.
                                File.Delete(copy_file);
                                File.Copy(e.FullPath, Path.Combine(target, e.Name));
                            }
                        }
    //under this is more code that didn't change
    }

EDIT: ONSTART:

protected override void OnStart(string[] args)
    {
      base.OnStart(args);
      this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
      ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
      fileSystemWatcher1.Path = source;
      fileSystemWatcher1.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
      fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);
      fileSystemWatcher1.Deleted += new FileSystemEventHandler(fileSystemWatcher1_Deleted);
      fileSystemWatcher1.Renamed += new RenamedEventHandler(fileSystemWatcher1_Renamed);

      this.fileSystemWatcher1.EnableRaisingEvents = true;
      this.fileSystemWatcher1.IncludeSubdirectories = true;
      ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
      logger("Service started "+ DateTime.Now);
    }

What am I doing wrong?

Upvotes: 1

Views: 5798

Answers (2)

Adrian Salazar
Adrian Salazar

Reputation: 5319

For the code, the only answer that anyone could give you are just "guessing". You better debug it yourself.

The easiest way to hit a break-point in a Windows Service is to put this line of code at the beginning of the OnStart method:

Debugger.Break();
  • Compile your service in Debug mode, so you can have all the necessary symbols in your executable.
  • Install your service
  • Start it from the service list.
  • You will get a prompt for debugging the "yourservicename.exe" program.
  • Say Yes-debug, Choose debugger.
  • Choose the correct Visual Studio version as your debugger.
  • Now you will be in the Debugger.Break line
  • Have fun

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

Usually this behavior indicates that the OnStart method ends without leaving any threads running. I have experienced that if an unhandled exception is thrown starting the service, the service doesn't go into Stopped state, but the service manager shows the "Starting service" window for 30 seconds and then fails saying it can't determine the service's state.

I'm not sure whether the FileSystemWatcher actually spawns a new thread that keeps running. You could (just to try it), also create a System.Timers.Timer that fires every 30 seconds in OnStart and stop that timer in OnStop. If the service keeps running then, you have to spawn a new thread for the FileSystemWatcher.

Usually, in OnStart you'd spawn a separate thread that does the service's work. Be it waiting for TCP connections, a timer that does things on a regular basis or any other kind of thread. If you don't do that, the service just stops as soon as there are no more threads active.

Upvotes: 1

Related Questions