> ' If this is the first hit > If lFirstHit Then > > ' Make sure to lock the thread otherwise, it two simultaneous hits are entering > ' here, which could be the case when the application starts, you could end up in a real mess > SyncLock loObjectLock > > ' If the application initialized has been completed by someone else, then, we can skip that part. The scenario here > ' is that the first one who get into there will do the setup. But, we need to avoid the other > ' one to setup again. > If Not oApp.lInitializeWeb Then > > ' Do initialization here > > oApp.lInitializeWeb = True > End If > > End SyncLock > > End If >>
public static class Bootstrapper { private static readonly object _lock = new object(); private static bool _initialized; public static void Bootstrap() { if (!_initialized) { lock (_lock) { if (!_initialized) { _initialized = true; StructureMapConfiguration.Initialize(); var startupTasks = ObjectFactory.GetAllInstances<IStartupTask>(); startupTasks.Each(task => task.Execute()); } } } } }I call this in my Application_Start event in the global.asax file. Looks like we had the same concern, but I'm handling it a bit differently by checking the _initialized variable before and after the lock.