.net - Does a Thread timeout or Thread.Sleep() for longtime close or abort a thread? -
i have windows service start tasks based on configuration.
each task start on own thread, tasks runes once per day.
i'm using thread.sleep()
, calculate time next run.
the problem after 2 or 3 days service still running tasks dont run.
the sleep period right, working fine , not getting exceptions.
is there make thread stop or sleep method abort thread?
this main function of thread
private void tasklifecycle() { try { // create task object. type tasktype = type.gettype(this.configurationelement.task); debtlogic.service.backgroundtask bgtask = activator.createinstance(tasktype) backgroundtask; bgtask.context = this; fireevent(createdeventkey, new backgroundtaskeventargs(bgtask)); fireevent(initilizingeventkey, new backgroundtaskeventargs(bgtask)); bgtask.initilize(); fireevent(initilizedeventkey, new backgroundtaskeventargs(bgtask)); // while thread alive, , did not abort run. while (this.currentthread.isalive) { try { //can run block if (canrun(bgtask)) { run(bgtask); } } catch (threadabortexception) { return; } catch (exception ex) { fireevent(erroreventkey,new backgroundtaskerrorargs(ex) ); } } bgtask.dispose(); } catch (threadabortexception) { return; } catch (exception ex) { fireevent(erroreventkey, new backgroundtaskerrorargs(ex)); } } private bool canrun(backgroundtask bgtask) { if (this.configurationelement.eventbasedtask) { return bgtask.canrun(); } timespan time; if (this.configurationelement.time != timespan.zero) { time = this.configurationelement.time - datetime.now.timeofday; if (time.totalmilliseconds < 0) time = time.add(new timespan(24, 0, 0)); } else { time = new timespan(0, 0, this.configurationelement.interval); } thread.sleep(time); return bgtask.canrun(); }
the run , initialize methods abstract methods calls task
instead of having thread sleeps long time. make short thread sleeps short time (1 minute) , each time checks if enough time has passed since last execution. more robust , lets track if system 'still alive'
Comments
Post a Comment