Topshelf - Service did not start in a timely manner
Asked Answered
R

0

6

Im trying to create a windows service using topshelf.I have managed to successfully create what i want and debug it.I have also managed to install the service using

myservice.exe install

But upon attempting to launch the service using the service manager i keep getting the following error

enter image description here

I have refereed to the following posts and tried running the timer from a new thread by adding a dummy method.But this also produces the same problem.

Topshelf window service giving Error 1053 when try to start the service

https://github.com/Topshelf/Topshelf/issues/183

My code:

class Program
        {
            static void Main(string[] args)
            {
                var rc = HostFactory.Run(x =>                                   //1
                {
                   // x.StartManually();
                    x.Service<testclass>(s =>                                   //2
                    {
                        x.StartManually();
                        s.ConstructUsing(name => new testclass());                //3
                        s.WhenStarted(tc => tc.Start());                         //4
                        s.WhenStopped(tc => tc.Stop());                          //5
                    });
                    x.RunAsLocalSystem();                                       //6

                    x.SetDescription("Sample Topshelf Host");                   //7
                    x.SetDisplayName("Stuff");                                  //8
                    x.SetServiceName("Stuff");                                  //9
                });                                                             //10

                var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());  //11
                Environment.ExitCode = exitCode;
            }
        }
    }

public class testclass
    {
     public testclass()
            {
                _timer = new System.Timers.Timer(1000) { AutoReset = true };
                _timer.Elapsed += _timer_Elapsed;
                startwatching();


            }
    void startwatching()
          {
              watcher.Created += new FileSystemEventHandler(copied);
              watcher.EnableRaisingEvents = true;
          }
    public bool Start()
    {


        var myThread = new Thread(new ThreadStart(foreverWhile));
        myThread.Start();
        return true;


    }

    public void Stop() { _timer.Stop(); }
    public void foreverWhile()
    {
        _timer.Start();

    }
    }

Im starting a filesystem watcher from the 'startwatching' method. What i'm i doing wrong? Please advice

Recover answered 9/7, 2018 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.