Example #1
0
        /// <summary>
        /// live for cache ;}
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CacheEngineLoop()
        {
            Debug.WriteLine("CacheEngine: start");
            int left = 0;

            OnTileCacheStart?.Invoke();

            bool startEvent = false;

            while (!abortCacheLoop)
            {
                try
                {
                    CacheQueueItem?task = null;

                    lock (tileCacheQueue)
                    {
                        left = tileCacheQueue.Count;
                        if (left > 0)
                        {
                            task = tileCacheQueue.Dequeue();
                        }
                    }

                    if (task.HasValue)
                    {
                        if (startEvent)
                        {
                            startEvent = false;

                            OnTileCacheStart?.Invoke();
                        }

                        OnTileCacheProgress?.Invoke(left);

                        #region -- save --
                        // check if stream wasn't disposed somehow
                        if (task.Value.Img != null)
                        {
                            Debug.WriteLine("CacheEngine[" + left + "]: storing tile " + task.Value + ", " + task.Value.Img.Length / 1024 + "kB...");

                            if ((task.Value.CacheType & CacheUsage.First) == CacheUsage.First && PrimaryCache != null)
                            {
                                if (cacheOnIdleRead)
                                {
                                    while (Interlocked.Decrement(ref readingCache) > 0)
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                                PrimaryCache.PutImageToCache(task.Value.Img, task.Value.Tile.Type, task.Value.Tile.Pos, task.Value.Tile.Zoom);
                            }

                            if ((task.Value.CacheType & CacheUsage.Second) == CacheUsage.Second && SecondaryCache != null)
                            {
                                if (cacheOnIdleRead)
                                {
                                    while (Interlocked.Decrement(ref readingCache) > 0)
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                                SecondaryCache.PutImageToCache(task.Value.Img, task.Value.Tile.Type, task.Value.Tile.Pos, task.Value.Tile.Zoom);
                            }

                            task.Value.Clear();

                            if (!boostCacheEngine)
                            {
#if PocketPC
                                Thread.Sleep(3333);
#else
                                Thread.Sleep(333);
#endif
                            }
                        }
                        else
                        {
                            Debug.WriteLine("CacheEngineLoop: skip, tile disposed to early -> " + task.Value);
                        }
                        task = null;
                        #endregion
                    }
                    else
                    {
                        if (!startEvent)
                        {
                            startEvent = true;

                            OnTileCacheComplete?.Invoke();
                        }

                        if (abortCacheLoop || noMapInstances || !WaitForCache.WaitOne(33333, false) || noMapInstances)
                        {
                            break;
                        }
                    }
                }
#if !PocketPC
                catch (AbandonedMutexException)
                {
                    break;
                }
#endif
                catch (Exception ex)
                {
                    Debug.WriteLine("CacheEngineLoop: " + ex.ToString());
                }
            }
            Debug.WriteLine("CacheEngine: stop");

            if (!startEvent)
            {
                OnTileCacheComplete?.Invoke();
            }
        }
Example #2
0
        /// <summary>
        /// live for cache ;}
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CacheEngineLoop()
        {
            int left = 0;

            if (OnTileCacheStart != null)
            {
                OnTileCacheStart();
            }

            bool startEvent = false;

            while (!abortCacheLoop)
            {
                try
                {
                    CacheQueueItem?task = null;

                    lock (tileCacheQueue)
                    {
                        left = tileCacheQueue.Count;
                        if (left > 0)
                        {
                            task = tileCacheQueue.Dequeue();
                        }
                    }

                    if (task.HasValue)
                    {
                        if (startEvent)
                        {
                            startEvent = false;

                            if (OnTileCacheStart != null)
                            {
                                OnTileCacheStart();
                            }
                        }

                        if (OnTileCacheProgress != null)
                        {
                            OnTileCacheProgress(left);
                        }

                        #region -- save --
                        // check if stream wasn't disposed somehow
                        if (task.Value.Img != null)
                        {
                            if ((task.Value.CacheType & CacheUsage.First) == CacheUsage.First && PrimaryCache != null)
                            {
                                if (cacheOnIdleRead)
                                {
                                    while (Interlocked.Decrement(ref readingCache) > 0)
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                                PrimaryCache.PutImageToCache(task.Value.Img, task.Value.Tile.Type, task.Value.Tile.Pos, task.Value.Tile.Zoom);
                            }

                            if ((task.Value.CacheType & CacheUsage.Second) == CacheUsage.Second && SecondaryCache != null)
                            {
                                if (cacheOnIdleRead)
                                {
                                    while (Interlocked.Decrement(ref readingCache) > 0)
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                                SecondaryCache.PutImageToCache(task.Value.Img, task.Value.Tile.Type, task.Value.Tile.Pos, task.Value.Tile.Zoom);
                            }

                            task.Value.Clear();

                            if (!boostCacheEngine)
                            {
#if PocketPC
                                Thread.Sleep(3333);
#else
                                Thread.Sleep(333);
#endif
                            }
                        }
                        else
                        {
                        }
                        task = null;
                        #endregion
                    }
                    else
                    {
                        if (!startEvent)
                        {
                            startEvent = true;

                            if (OnTileCacheComplete != null)
                            {
                                OnTileCacheComplete();
                            }
                        }

                        if (abortCacheLoop || noMapInstances || !WaitForCache.WaitOne(33333, false) || noMapInstances)
                        {
                            break;
                        }
                    }
                }
#if !PocketPC
                catch (AbandonedMutexException)
                {
                    break;
                }
#endif
                catch
                {
                }
            }

            if (!startEvent)
            {
                if (OnTileCacheComplete != null)
                {
                    OnTileCacheComplete();
                }
            }
        }