I hope to formulate this post in a legitimate StackOverflow question, as I would really like a solid understanding of this scenario, but I could definitely see if it is too localized or considered an “opinion”.
Here is my scenario: when I load my web application, I load a whole bunch of data from the database and cache it. The problem is that this process takes about 10-15 seconds and creates a delay on the first start of the web server. This is annoying during development, and also causes some problems when I give up the web server in production (since this is a new site, I often hotly fix small errors when I find them, or reset IIS settings).
I had to wonder - can I offload this work to a new thread when the application starts, and this happens in the background, as other users use this site? Obviously, some functions will not work for 10-15 seconds when loading the site, but I can process this condition or block until the data becomes available. At first I thought not. The web server was about to either terminate these threads if the request ended, or was blocked until these threads ended. I decided to write a small test application to test this theory:
public class Global : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { Thread thread = new Thread(LoadData); thread.Start(); } private void LoadData() { for (int i = 0; i < 100; i++) { Trace.WriteLine("Counter: " + i.ToString()); Thread.Sleep(1000); } } }
When the application starts, I started a new thread, and its number will be equal to 100. To my great surprise, I immediately got to the home page and in the Visual Studio debug output window, I could see the counter of additional numbers up. I was really surprised that this works.
My questions:
Firstly, are there any problems with this? Is he asking for trouble and will something explode? Will this change change between web servers or versions of IIS, because maybe they use different stream models? I am looking for general information about this design.
source share