c# - Watchdog for COM Thread -
i developing wcf application not adhere idea world of true soa in have few basic calls.
1) start process 2) check if process still running 3) results
this fine, in order execution return 'start process' running code in separate thread using
othread = new thread(new threadstart(wbrt.calculate));
the problem has been known (though rare) com call hog , sit @ 50~100% cpu , consume lots of memory. there nothing can prevent this.
what want have watchdog thread kill off com call if still running after time (say 5 minutes).
is there best practice here? have been reading following blog:
http://weblogs.asp.net/israelio/archive/2004/06/19/159985.aspx
in makes use of 'autoresetevent' , 'waitone' problem here waitone blocking i'd have have thread within thread.
could use simple thread timer here? saw timer execute if thread has closed (i.e. finished ok) problem if use flag or stop use of dead objects?
also creating com object using activator.createinstance tried aborting thread, not kill com object , have since read abort bad.
my solution pid of com instance , kill, working on assumption if timeout has hit diplomacy thread has gone out of window.
any thoughts received!
easiest way 1 manualresetevent calling waitany() timeout value.
manualresetevent _evtactor = new manualresetevent(); public void start() { this._evtactor.reset(); threadpool.queueuserworkitem(new waitcallback(dostuff)); int result = manualresetevent.waitany( new waithandle[] { this._evtactor }, 30 * 1000); // wait 30sec if (result == manualresetevent.waittimeout) { console.writeline("timeout occurred!"); } else { console.writeline("done!"); } } public void dostuff() { console.writeline("doing stuff."); thread.sleep(45 * 1000); // sleep 45sec; this._evtactor.set(); }
Comments
Post a Comment