c# - Replacing Thread.Suspend and Thread.Resume in a windows service -


we have service following basic workflow:

1) starts, reads config settings , performs calculations in large loop.

2) each iteration of loop, needs able check if service has been told stop. performs database fetches, calculations stores results. not confident on how code done wrt sql transactions @ stage, happy assume checking service stop @ start of each iteration.

3) after performing iterations, service "sleeps" period of time. 5 minutes. 12 hours. needs able "stop" in sleep period!

currently performed following:

private int threadsleepminutes = 60;  private readonly mutex mutterminateservice = new mutex(false);  private thread servicethread;  private thread servicestopthread;  // use flag allow start op wait servicestopthread // going before continuing create main loop thread private volatile bool stopservice = true;  public void start() {     this.servicestopthread = new thread(this.runservicestopthread);     this.servicestopthread.isbackground = true;     this.servicestopthread.start();      while (stopservice)     {         thread.sleep(100);     }      // things renamed anonymise... idea!     this.servicethread = new thread(this.bigloopmethod);     this.servicethread.isbackground = true;     this.servicethread.start(); }  public void stop() {     // release mutex terminate service     servicestopthread.resume();      // wait 5s max     int timeout = 5000;     while (this.servicethread.isalive && timeout > 0)     {         thread.sleep(100);         timeout -= 100;     } }  private void runservicestopthread() {     // guarantee same thread takes mutex     // , releases in dot net 4, both ops in single thread!     // dot net 4 start() , stop() on different threads.     mutterminateservice.waitone();     stopservice = false;      // suspend ourself     servicestopthread.suspend();      // release mutex     mutterminateservice.releasemutex(); }  public void bigloopmethod() {     try     {             {           bool moreoperationstogo = true; // dummy flags , 'stuff' methods here           while (moreoperationstogo && !mutterminateservice.waitone(0))           {               dostuff();           }            // using mutex here sleep nicely - event driven.           // gracefully continues after timeout , gracefully exits if            // triggered mutex.       }       while (!mutterminateservice.waitone(this.threadsleepminutes * 60000));     }     catch (exception ex)     {         // exception handling & logging here     } } 

now messages saying suspend , resume deprecated. in situation, know exactly code suspend run on since call suspended it! resume, know going do. reason done in first place because mutex worked fine in start() , stop() in dot net 3.5 dot net 4.0 changed start() , stop() in different threads , marked workaround obsolete!

is there nice way, non-obsolete way of doing this?

thanks

unless using mutex inter-process communication, i.e. cancelling worker thread process - believe there easier way implement worker thread cancellation in .net 4.0. can use cancellation token, , wait timeout on - signal if token cancelled. complete solution (partially using code) below:

using system; using system.threading;  class app {   static void main()   {     var t = new test();     t.start();     thread.sleep(10000);     console.writeline("aborting");     t.stop();   } }  class test { private int threadsleepminutes = 60;  private thread servicethread;  private cancellationtokensource tokensource;  public void start() {     // things renamed anonymise... idea!     this.tokensource = new cancellationtokensource();     this.servicethread = new thread(this.bigloopmethod);     this.servicethread.isbackground = true;     this.servicethread.start(); }  public void stop() {     tokensource.cancel();      // wait 5s max     int timeout = 5000;     if (!servicethread.join(timeout))     {       servicethread.abort();     } }  public void bigloopmethod() {     try     {       var token = tokensource.token;             {           int operationstogo = 4; // dummy flags , 'stuff' methods here           while (operationstogo > 0 && !token.iscancellationrequested)           {               console.writeline("work");               thread.sleep(1000);//dostuff();               operationstogo--;           }           console.writeline("no more work");       }       while (!token.waithandle.waitone(this.threadsleepminutes * 60000));     }     catch (exception ex)     {         // exception handling & logging here     } } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -