Exemple #1
0
        private void dataRefreshThread()
        {
            m_evtRefreshRunning.Set();
            DatasetFactory factory = new DatasetFactory(m_factory);
            Stopwatch      sw      = new Stopwatch();

            try
            {
                sw.Start();

                m_log.WriteLine("Starting refresh of " + m_nReplacementBatch.ToString("N0") + " items...");

                List <Tuple <int, SimpleDatum> > rgReplace = new List <Tuple <int, SimpleDatum> >();
                List <Tuple <int, DbItem> >      rgItems   = new List <Tuple <int, DbItem> >();
                List <DbItem> rgDbItems = new List <DbItem>();

                // Load the replacement set.
                for (int i = 0; i < m_nReplacementBatch; i++)
                {
                    int nIdx   = m_random.Next(m_rgImages.Length);
                    int?nLabel = null;

                    if (m_rgImages[nIdx] != null)
                    {
                        nLabel = m_rgImages[nIdx].Label;
                    }

                    DbItem img = m_refreshManager.GetNextImageId(nLabel);
                    rgItems.Add(new Tuple <int, DbItem>(nIdx, img));
                    rgDbItems.Add(img);

                    if (sw.Elapsed.TotalMilliseconds > 1000)
                    {
                        if (m_evtRefreshCancel.WaitOne(0))
                        {
                            return;
                        }

                        sw.Restart();
                    }
                }

                // Get the Datums, ordered by ID.
                List <SimpleDatum> rgImg = m_factory.GetImagesAt(rgDbItems, m_evtCancel);
                if (rgImg == null)
                {
                    return;
                }

                rgImg   = rgImg.OrderBy(p => p.ImageID).ToList();
                rgItems = rgItems.OrderBy(p => p.Item2.ID).ToList();

                if (rgImg.Count != rgItems.Count)
                {
                    List <Tuple <int, DbItem> > rgItems1 = new List <Tuple <int, DbItem> >();
                    int nIdx = 0;
                    for (int i = 0; i < rgImg.Count; i++)
                    {
                        while (nIdx < rgItems.Count && rgItems[nIdx].Item2.ID < rgImg[i].ImageID)
                        {
                            nIdx++;
                        }

                        if (rgItems[nIdx].Item2.ID == rgImg[i].ImageID)
                        {
                            rgItems1.Add(rgItems[nIdx]);
                            nIdx++;
                        }
                    }

                    rgItems = rgItems1;
                }

                for (int i = 0; i < rgItems.Count; i++)
                {
                    rgReplace.Add(new Tuple <int, SimpleDatum>(rgItems[i].Item1, rgImg[i]));
                }

                lock (m_syncObj)
                {
                    int nMismatchCount = 0;

                    for (int i = 0; i < rgReplace.Count; i++)
                    {
                        int nIdx = rgReplace[i].Item1;
                        if (m_rgImages[nIdx] != null && m_rgImages[nIdx].Label != rgReplace[i].Item2.Label)
                        {
                            nMismatchCount++;
                        }
                        else
                        {
                            m_rgImages[nIdx] = rgReplace[i].Item2;
                        }
                    }

                    if (nMismatchCount > 0)
                    {
                        m_log.WriteLine("WARNING: " + nMismatchCount.ToString("N0") + " label mismatches found!");
                    }
                }
            }
            finally
            {
                m_evtRefreshRunning.Reset();
                m_evtRefreshDone.Set();
            }
        }