internal void EnsureLoaded() { #if FALSE // If we're fully loaded, just return. if (_loadState == LoadState.Loaded) { return; } // If we're loading or not loaded, we need to take this lock. if (!_typeDb.BeginModuleLoad(this, 10000)) { Debug.Fail("Timeout loading {0}", _modName); //throw new InvalidOperationException("Cannot load module at this time"); return; } try { // Ensure we haven't started/finished loading while waiting if (_loadState != LoadState.NotLoaded) { return; } // Mark as loading now (before it completes), if we have circular // references we'll fix them up after loading // completes. _loadState = LoadState.Loading; using (var stream = new FileStream(_dbFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { Dictionary <string, object> contents = null; try { contents = (Dictionary <string, object>)Unpickle.Load(stream); } catch (ArgumentException) { _typeDb.OnDatabaseCorrupt(); } catch (InvalidOperationException) { // Bug 511 - http://pytools.codeplex.com/workitem/511 // Ignore a corrupt database file. _typeDb.OnDatabaseCorrupt(); } if (contents != null) { LoadModule(contents); } } } catch (FileNotFoundException) { // if the file got deleted before we've loaded it don't crash... } catch (IOException) { // or if someone has locked the file for some reason, also don't crash... } finally { // Regardless of how we finish, mark us as loaded so we don't // try again. _loadState = LoadState.Loaded; _typeDb.EndModuleLoad(this); } #endif }
internal void EnsureLoaded() { // If we're fully loaded, just return. if (_loadState == LoadState.Loaded) { return; } // If we're loading or not loaded, we need to take this lock. if (!_typeDb.BeginModuleLoad(this, 10000)) { Debug.Fail(string.Format("Timeout loading {0}", _modName)); //throw new InvalidOperationException("Cannot load module at this time"); return; } try { // Ensure we haven't started/finished loading while waiting if (_loadState != LoadState.NotLoaded) { return; } // Mark as loading now (before it completes), if we have circular // references we'll fix them up after loading // completes. _loadState = LoadState.Loading; // Will be set to true if we should delete this file and then // trigger a corruption notification. bool deleteFile = false; using (var stream = new FileStream(_dbFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { Dictionary <string, object> contents = null; try { contents = (Dictionary <string, object>)Unpickle.Load(stream); } catch (ArgumentException) { _typeDb.OnDatabaseCorrupt(); } catch (InvalidOperationException) { // Bug 511 - http://pytools.codeplex.com/workitem/511 // Ignore a corrupt database file. _typeDb.OnDatabaseCorrupt(); } if (contents != null) { object obj; string filename; if (contents.TryGetValue("filename", out obj)) { filename = obj as string; if (!string.IsNullOrEmpty(filename) && !File.Exists(filename)) { // Issue 2358 - must refresh DB after it moves // on disk. deleteFile = true; } } LoadModule(contents); } } if (deleteFile) { // Only notify if we actually remove the file try { File.Delete(_dbFile); _typeDb.OnDatabaseCorrupt(); } catch (IOException) { } catch (UnauthorizedAccessException) { } } } catch (FileNotFoundException) { // if the file got deleted before we've loaded it don't crash... } catch (IOException) { // or if someone has locked the file for some reason, also don't crash... } finally { // Regardless of how we finish, mark us as loaded so we don't // try again. _loadState = LoadState.Loaded; _typeDb.EndModuleLoad(this); } }