5
\$\begingroup\$

I used the information from the article "Auto-Start ASP.NET Applications (VS 2010 and .NET 4.0 Series)" to create a background task that runs on an infinite loop. Ultimately, this program is going to be consuming a REST API to get data from a remote web site and insert records into my database. For the time being, I am just doing a basic insert.

public class PreWarmCache : System.Web.Hosting.IProcessHostPreloadClient
{
    private static BackgroundTaskContext db = new BackgroundTaskContext();
    private static Timer _timer;

    public void Preload(string[] parameters)
    {
        // Perform initialization and cache loading logic here...
        try
        {
            var doStuff = new DoStuff();
            doStuff.DoStuffName = "Test from PRELOAD INIT " + DateTime.Now;
            db.DoStuffs.Add(doStuff);
            db.SaveChanges();
        }
        catch
        {
        }

        var timeInMilliseconds = 0;
        _timer = new Timer(Callback, null, timeInMilliseconds, Timeout.Infinite);

    }

    public static void Callback(Object state)
    {
        var timeInMilliseconds = 5000;
        Stopwatch watch = new Stopwatch();

        watch.Start();

        // Update the database
        try
        {
            var doStuff = new DoStuff();
            doStuff.DoStuffName = "Test from CALLBACK " + DateTime.Now;
            db.DoStuffs.Add(doStuff);
            db.SaveChanges();
        }
        catch
        {
        }

        _timer.Change(Math.Max(0, timeInMilliseconds - watch.ElapsedMilliseconds), Timeout.Infinite);
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Interesting. I've been keeping an eye on the Auto-Start feature but haen't gotten a chance to use it much. Any idea how it does with these limitations of IIS applications? \$\endgroup\$ Jun 24, 2014 at 18:24
  • \$\begingroup\$ Not yet. I am just trying to get the REST API code working. Debugging is difficult. I am not sure how to debug this once deployed to IIS and the code does not seem to be working. I know I can attach a worker process. But, it's not really helping me. \$\endgroup\$
    – ADH
    Jun 24, 2014 at 20:29
  • \$\begingroup\$ heavy logging always helps me in that scenario. \$\endgroup\$ Jun 24, 2014 at 20:56
  • \$\begingroup\$ you dont have to use IProcessHostPreloadClient , u want data to be prefetched every 5 min. so create a window service and fetch the data , insert into you db \$\endgroup\$
    – Paritosh
    Aug 9, 2014 at 16:57

2 Answers 2

2
\$\begingroup\$

There are some things that you could have done better certainly.

My first pick is the class name DoStuff. Please try pick a more meaningful name like Action or Task.

In this class you seem to be appending the current time to the property DoStuffName (which should also be renamed accordingly). You could pass the name in the constructor and append the current time instead, like:

public class Task{
   public Task(string name){
      Name = name + " " + DateTime.Now;
   }
   public string Name{
     get; private set;
   }  
}

You also seem to have one of the most evil bad practises of programming - the empty catch block. Be sure to, at least, log the exception somewhere.

\$\endgroup\$
1
  • \$\begingroup\$ You should preferably throw the exception again, even if you log it. Unless you've handled the situation that caused the exception, you should rethrow the exception. \$\endgroup\$
    – Nick Udell
    Dec 2, 2014 at 12:02
2
\$\begingroup\$

Naming

I don't like underscore notation, but it's regularly debated and is mostly a matter for personal preference, however everybody agrees that if you pick a naming scheme, stick with it.

private static BackgroundTaskContext db = new BackgroundTaskContext();
private static Timer _timer;

It should be _db or timer. Not both.

Static

Your use of static is confusing here. Do you really only want one timer and background task context for every instance of PreWarmCache? If you only want one instance, make it so, do not allow multiples and then have them all overwriting each other.

There also seems to be a case where calling Preload multiple times will just reset the timer and only call callback with the state from the last preload call.

Var

Mostly, you're pretty good for using var correctly, but you don't use it consistently.

Stopwatch watch = new Stopwatch();

Should be

var watch = new Stopwatch();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.