public LoadSequence(CryptoRandom random, int nCount, int nImageCount, RefreshManager refresh) { // When using load limit (count < image count), load item indexes in such // a way that balances the number of items per label. if (nCount < nImageCount) { Dictionary <int, List <DbItem> > rgItemsByLabel = refresh.GetItemsByLabel(); List <int> rgLabel = rgItemsByLabel.Where(p => p.Value.Count > 0).Select(p => p.Key).ToList(); for (int i = 0; i < nCount; i++) { int nLabelIdx = random.Next(rgLabel.Count); int nLabel = rgLabel[nLabelIdx]; List <DbItem> rgItems = rgItemsByLabel[nLabel]; int nItemIdx = random.Next(rgItems.Count); DbItem item = rgItems[nItemIdx]; m_rgLoadSequence.Add(item.Index); rgLabel.Remove(nLabel); if (rgLabel.Count == 0) { rgLabel = rgItemsByLabel.Where(p => p.Value.Count > 0).Select(p => p.Key).ToList(); } } refresh.Reset(); } // Otherwise just load all item indexes. else { for (int i = 0; i < nCount; i++) { m_rgLoadSequence.Add(i); } } }
/// <summary> /// The dataLoadThread is responsible for loading the data source images in the background. /// </summary> private void dataLoadThread() { m_evtRunning.Set(); DatasetFactory factory = new DatasetFactory(m_factory); int? nNextIdx = m_loadSequence.GetNext(); Stopwatch sw = new Stopwatch(); if (m_refreshManager != null) { m_refreshManager.Reset(); } try { sw.Start(); List <int> rgIdxBatch = new List <int>(); int nBatchSize = getBatchSize(m_src); if (m_nLoadedCount > 0) { throw new Exception("The loaded count is > 0!"); } factory.Open(m_src); m_log.WriteLine(m_src.Name + " loading " + m_loadSequence.Count.ToString("N0") + " items..."); while (nNextIdx.HasValue || rgIdxBatch.Count > 0) { if (nNextIdx.HasValue) { rgIdxBatch.Add(nNextIdx.Value); } if (rgIdxBatch.Count >= nBatchSize || !nNextIdx.HasValue) { List <RawImage> rgImg; if (m_refreshManager == null) { rgImg = factory.GetRawImagesAt(rgIdxBatch[0], rgIdxBatch.Count); } else { rgImg = factory.GetRawImagesAt(rgIdxBatch, m_evtCancel); } if (rgImg == null) { break; } for (int j = 0; j < rgImg.Count; j++) { SimpleDatum sd = factory.LoadDatum(rgImg[j]); if (m_refreshManager != null) { m_refreshManager.AddLoaded(sd); } m_rgImages[m_nLoadedCount] = sd; m_nLoadedCount++; if (sw.Elapsed.TotalMilliseconds > 1000) { if (m_log != null && !m_bSilent) { double dfPct = m_nLoadedCount / (double)m_rgImages.Length; m_log.Progress = dfPct; m_log.WriteLine("Loading '" + m_src.Name + "' at " + dfPct.ToString("P") + " (" + m_nLoadedCount.ToString("N0") + " of " + m_rgImages.Length.ToString("N0") + ")..."); } int nWait = WaitHandle.WaitAny(m_rgAbort.ToArray(), 0); if (nWait != WaitHandle.WaitTimeout) { return; } sw.Restart(); } } rgIdxBatch = new List <int>(); } nNextIdx = m_loadSequence.GetNext(); } if (rgIdxBatch.Count > 0) { m_log.FAIL("Not all images were loaded!"); } } finally { factory.Close(); factory.Dispose(); m_evtRunning.Reset(); m_evtDone.Set(); } }