c# - how to run a background thread properly for a MVC app on shared host? -


i need run background thread mvc 4 app, thread wakes every hour or delete old files in database, goes sleep. method below:

//delete old files database public void cleandb() {     while (true)     {         using (userzipdbcontext db = new userzipdbcontext())         {             //delete old files             datetime timepoint = datetime.now.addhours(-24);             foreach (userzip file in db.userfiles.where(f => f.uploadtime < timepoint))             {                 db.userfiles.remove(file);             }             db.savechanges();         }         //sleep 1 hour         thread.sleep(new timespan(1, 0, 0));     } } 

but should start thread? answer in this question creates new thread , start in global.asax, post mentions "asp.net not designed long running tasks". app run on shared host don't have admin privilege, don't think can install seperate program task.

in short,

  1. is okay start thread in global.asax given thread doesn't (sleep of time , small db)?

  2. i read risk of approach thread might killed (though not sure why). how can detect when thread killed , can do?

  3. if bad idea, else can on shared host?

thanks!

update

@usr mentioned methods in application_start can called more once , suggested using lazy. before read on topic, thought of approach. calling simpleprint.startsingletonthread() multiple times instantiate single thread (i think). correct?

public class simpleprint {     private static thread tinstance = null;      private simpleprint()     {     }      public static void startsingletonthread()     {         if (tinstance == null)         {             tinstance = new thread(new threadstart(new simpleprint().printstuff));             tinstance.start();         }     }      private void printstuff()     {         datetime d = datetime.now;         while (true)         {             console.writeline("thread started @ " + d);             thread.sleep(2000);         }     } }  

asp.net not designed long-running tasks, yes. because work , data can lost @ time when worker process restarts.

you not keep state between iterations of task. task can safely abort @ time. safe run in asp.net.

starting thread in application_start problem because function can called multiple times (surprisingly). suggest make sure start deletion task once, example using lazy<t> , accessing value property in application_start.

static readonly lazy<object> workerfactory =      new lazy<object>(() => { startthread(); return null; });  application_start:   var dummy = workerfactory.value; 

for reason cannot think of better init-once pattern right now. nothing without locks, volatile or interlocked solutions of last resort.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -