concurrency - ASP.NET background worker thread -
a have asp.net 2.0 web application should allow sending emails. have windows service sends emails immediately. web application composes email message according template , put in msmq , service there.
the problem composing message template can take time , don't want user wait while message composed , passed service.
i think background process listen internal queue of notifications requests. if queue empty process nothing, message appeared begins process message. want have single process not create lot of threads.
currently idea write task scheduler, contain queue of notifications requests. when new item added queue scheduler checks whether process of sending notifications running. if yes, add request queue. otherwise creates new thread read queue until empty , perform notification requests.
my concern need sure thread not die after asp.net finish response client because parent thread thread. , question best way (or possible it)?
p.s. ok thread dies if iis recycles asp.net process due user inactivity.
i use class below base class. inherit class , put logic inside it. store instance of class in asp.net cache reference kept , can find it. purposes after inheriting class inside of executeprocess create infinite while loop "while(true)", put delay @ top/bottom of loop "thread.sleep(500)", or that. each loop check messages in queue.
imports system.threading public mustinherit class longrunningprocess public readonly property running() boolean return _running end end property public readonly property success() boolean return _success end end property public readonly property exception() exception return _exception end end property public readonly property starttime() datetime return _starttime end end property public readonly property endtime() datetime return _endtime end end property public readonly property args() object return _args end end property protected _running boolean = false protected _success boolean = false protected _exception exception = nothing protected _starttime datetime = datetime.minvalue protected _endtime datetime = datetime.minvalue protected _args() object = nothing protected withevents _thread thread private _locker new object() public sub execute(byval arguments object) synclock (_locker) 'if process not running, then...' if not _running 'set running true' _running = true 'set start time now' _starttime = datetime.now 'set arguments' _args = arguments 'prepare process in new thread' _thread = new thread(new threadstart(addressof executeprocess)) 'start thread' _thread.start() end if end synclock end sub protected mustoverride sub executeprocess() end class
Comments
Post a Comment