c# - Windows Service installed. But start, stop buttons disabled -


this question has answer here:

i created windows service. installed installutil.exe success, when press start button appears progress bar "starting", stopped @ 50 percent. have cancel. after cancel service works perfect, start, stop , pasue commands disabled. if want stop service, have uninstall service. can reason ?

main method:

static void main()     {         servicebase[] servicestorun;         servicestorun = new servicebase[]              {                  new telegrambotservice()             };         servicebase.run(servicestorun);     } 

it service class:

partial class telegrambotservice : servicebase {     private static logger logger = logmanager.getcurrentclasslogger();     public telegrambotservice()     {         initializecomponent();     }      protected override void onstart(string[] args)     {         try         {             runbot().wait();             logger.info("start windows service");         }         catch(exception ex)         {             logger.error(ex.message);         }     }      private async task runbot()     {         try         {            logger.info("start telegram bot");           //var electionsbot = new electionsinfo();           var electionsbot = new electionscount();           await electionsbot.start();         }         catch(exception ex)         {             logger.error(ex.message);         }     }      protected override void onstop()     {         logger.info("stop windows service");     } } 

the onstart method must finish within 10 seconds, otherwise service control manager thinks it's hung. it's waiting indefinitely runbot task finish.

i've described good way deal approach. onstart method, launches service's controller thread:

// scm requests service start using own thread. // method must complete within 10 seconds of // starting. otherwise scm diagnoses hang. protected override void onstart(string[] args) {     appdomain currentdomain = appdomain.currentdomain;     currentdomain.unhandledexception += new unhandledexceptioneventhandler(this.unhandledexceptionfilter);      this.threadcontroller = new thread(new threadstart(controllerthreadrunning));     this.threadcontroller.name = thread_name_controller;     this.threadcontroller.start();     base.onstart(args); } 

this controller thread:

// invoked when controller thread starts.  private void controllerthreadrunning() {     // , we're on our way.     while ( !this.servicestoprequested )     {         // start real work , block until finishes or crashes.         var realwork = this.launchworkasync();         realwork.wait();         // if scm didn't request service stop, assumption          // real work crashed , needs restarted.         if ( !this.servicestoprequested )         {             this.pausecontrollerthread("pause before restarting work cycle.", this.restartdelay);         }     }      // service stop.     this.cleanup(); } 

this asynchronously launches service's real work:

// method handles ceremony around real work of service. private async task launchworkasync() {     try      {         // setup progress reporting.         var progressreport = new progress<string>                 (progressinfo => { this.applog.information(progressinfo, "servicemain.launchworkasync"); });         var progress = progressreport iprogress<string>;         // launch time.         await task.factory.startnew( () => this.dowork(progress), taskcreationoptions.longrunning );     }      // report exception raised during work cycle.     catch (exception ex)     {         this.applog.error(string.concat("work cycle crashed", environment.newline,                                          ex.gettype().fullname, environment.newline,                                          ex.message, environment.newline,                                          ex.stacktrace));     }      return; } 

and service's real work done:

// service's real work done. // work cycles continuously until it's asked stop. // if work crashes unhandled exception,  // controller thread restart after appropriate delay. private void dowork(iprogress<string> progress) {     while (!this.servicestoprequested)     {         thread.sleep(5000);     // simulated work cycle.         progress.report("completed current cycle of work.");     } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -