c# - What to do with a worker thread in the OnStop method in a basic Windows service? -


i have basic windows service performs tasks, executed periodically in endless loop: "executeserviceworkermethods()". starting endless loop via worker thread onstart() method below:

onstart(string[] args) {        workerthread = new thread(executeserviceworkermethods);        workerthread.name = "serviceworkerthread";        workerthread.isbackground = false;        workerthread.start(); } 

now wondering worker thread in onstop() method?

my endless loop looks this:

private void executeserviceworkermethods() {       while (!servicestopped)       {      work....            while (servicepaused)           {               thread.sleep(sleeptimemillisecondswhileservicepaused);           }       thread.sleep(sleeptimemillisecondswhileservicenotstopped);       } } 

remember basic. want able start , stop windows service.

it appears code follows code found here pretty closely.

you should able use this:

protected override void onstop() {    // flag tell worker process stop    servicestopped = true;     // give little time finish pending work    workerthread.join(new timespan(0,2,0)); } 

the call .join cause thread terminate.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -