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
Post a Comment