c# - How can long running thread work inside web application -
so have inside mvc controller method following code :
public actionresult processfile () { threadstart threadstart = new threadstart ( ()=>{ // doing long processing takes 20 minute } ); thread thread = new thread(threadstart); thread.start(); } the problem here when more 1 request sent controller method thread killed. need thread stay working untill processing ends , , seem matter of resources resources taken less threads can stay working . if run process windows application or service works , having problem when started web application .
ideally should not create threads (for long running background jobs) off web requests in iis 7.
the threads stopped should app pool/appdomain restart, happen number of reasons such changes contents of bin folder, changes web.config, period of inactivity, resource threshold reached (e.g. memory usage). in fact appdomain restart periodically default no other reason has been alive x amount of time.
for reason long running tasks should implemented using job queue , background job runner, e.g. console app or windows service (importantly, not running in context of iis), processing jobs.
the web request should add job queue allowing background job runner pick jobs appear.
a similar question has been answered here can use threads carry out long-running jobs on iis?
edit: seriously not recommended alternative
if insist or have no alternative run background job in iis process, need @ allowing background job spontaneously stopped, , allowing restart left off when stopped.
you can have url can polled trigger restart of unfinished jobs.
i have seen work in past, implementation vary in complexity based on background job entails.
you find server may begin have performance issues background jobs started , killed repeatedly.
Comments
Post a Comment