public void OnImageLoaded(string nextSeriesInstanceUid, string nextSopInstanceUid)
        {
            PrefetchQueueItem item = new PrefetchQueueItem();

            item.SeriesInstanceUid = nextSeriesInstanceUid;
            item.SopInstanceUid    = nextSopInstanceUid;
            _prefetchQueue.Enqueue(item);
            _enqueueEvent.Set();
        }
        private void Run()
        {
            while (!_stop)
            {
                while (_prefetchQueue.Count > 0)
                {
                    PrefetchQueueItem item = _prefetchQueue.Dequeue();
                    if (item != null)
                    {
                        Prefetch(item);
                    }
                }

                _enqueueEvent.WaitOne(1000, true);
            }

            Platform.Log(LogLevel.Debug, "Prefetch has stopped. Series {0}", _seriesInstanceUid);
        }
        private void Prefetch(PrefetchQueueItem item)
        {
            try{
                string key = string.Format("ImageStreamPrefetchStream/{0}/{1}/{2}/{3}",
                                           _storage.ServerPartition.AeTitle, _storage.StudyInstanceUid,
                                           item.SeriesInstanceUid, item.SopInstanceUid);

                for (int i = 0; i < 5; i++)
                {
                    Stream stream = HttpRuntime.Cache[key] as Stream;
                    if (stream != null)
                    {
                        // already pre-open
                        break;
                    }

                    else
                    {
                        try
                        {
                            stream = new BufferedStream(File.OpenRead(_storage.GetSopInstancePath(item.SeriesInstanceUid, item.SopInstanceUid)));
                            HttpRuntime.Cache.Insert(key, stream, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, PrefetchUnloaded);
                            break;
                        }
                        catch (FileNotFoundException)
                        {
                            throw;
                        }
                        catch (IOException)
                        {
                            Random rand = new Random();
                            Thread.Sleep(rand.Next(100, 500));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Platform.Log(LogLevel.Error, ex, "Error occurred during prefetch SOP: {0}", item.SopInstanceUid);
            }
        }