Hello. I have a short lock guarded section in a method (that serves the request entirely) that makes all initializations (etc. log-related). So only 1 thread can be there at time. In this section I also load system data from database if not loaded. This is naturally executed only on 1st request and it does not matter it takes time and no threads can propagate since it's done only once (by dummy request).
static public void LoadAllSystemData() { SystemData newData = new SystemData(); //own type (etc. Hashtables in Hashtables). LoadTables(ref newData); LoadClasses(ref newData); LoadAllSysDescrs(ref newData); LoadFatFields(ref newData); LoadAllFields(ref newData); _allData = newData; }
After the lock-guarded section the system data is accessed from concurrent threads only by reading and no locks are needed:
static public Hashtable GetTables() { return _allData.Tables; }
Now the lock guarded section must have method that checks if system data is older than 24h and refresh it. If it done just by calling method (from lock guarded section) below that thread takes a long time and no other thread can enter the lock guarded section.
static public void CheckStatus() { DateTime timeStamp = DateTime.Now; TimeSpan span = timeStamp.Subtract(_cacheTimeStamp); if (span.Hours >= 24) { LoadAllSystemData(); _cacheTimeStamp = DateTime.Now; } }
My questions are:
How to spawn a non-threadpool thread best way to handle IO so the threadpool worker thread can propagate and all the threads spend minimum time in lock guarded section?
Is the _allData = newData; in LoadAllSystemData atomic? If it is, it feels the best way to implement that so GetXxx-methods like GetTables do not need any locking!
Is there any way to get LoadAllSystemData to be called before requests? For example on iisreset?
Thanks in advance for your answers!