private void GetTile(TileCoordWrap tileWrap)
 {
     try
     {
         Interlocked.Increment(ref _currTileIndex);
         using (Stream stream = _source.GetTile(tileWrap.Tile))
         {
             string filePath = _tilePathBuilder.BuildTilePath(tileWrap.Tile);
             using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
             {
                 byte[] buffer = new byte[8192];//8K
                 int    ret    = stream.Read(buffer, 0, buffer.Length);
                 while (ret > 0)
                 {
                     fs.Write(buffer, 0, ret);
                     ret = stream.Read(buffer, 0, buffer.Length);
                 }
                 tileWrap.OnSuccess?.Invoke();
                 OnTileLoaded(tileWrap.Tile);
             }
         }
     }
     catch (Exception ex)
     {
         tileWrap.OnFailed.Invoke(tileWrap.Tile, ex);
     }
     finally
     {
         tileWrap.OnFinally.Invoke(tileWrap.Tile);
     }
 }
Beispiel #2
0
        public void Start()
        {
            if (!_isStop)
            {
                _blockingQueue = new BlockingCollection <TileCoord>(_threadCount);
                _tokenSource   = new CancellationTokenSource();
                var token       = _tokenSource.Token;
                var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning);

                for (int i = 0; i < _threadCount; i++)
                {
                    Task workThread = taskFactory.StartNew(() =>
                    {
                        while (!_isStop)
                        {
                            token.ThrowIfCancellationRequested();

                            TileCoord tileCoord = null;
                            if (_blockingQueue.TryTake(out tileCoord, 100))
                            //tileCoord = _blockingQueue.Take();
                            //if (tileCoord != null)
                            {
                                using (Stream stream = _source.GetTile(tileCoord))
                                {
                                    string filePath = _tilePathBuilder.BuildTilePath(tileCoord);
                                    using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                    {
                                        int ret       = 0;
                                        byte[] buffer = new byte[8192];//8K
                                        ret           = stream.Read(buffer, 0, buffer.Length);
                                        while (ret > 0)
                                        {
                                            fs.Write(buffer, 0, ret);
                                            ret = stream.Read(buffer, 0, buffer.Length);
                                        }
                                        OnTileLoaded(tileCoord);
                                    }
                                }
                            }
                            //else
                            //{
                            //    token.WaitHandle.WaitOne(100);
                            //}
                        }
                    }, token);
                }

                List <Extent> fullTileRange = _source.TileGrid.TileRanges;
                for (int z = 0; z < fullTileRange.Count; z++)
                {
                    _tilePathBuilder.BuildZoomFold(z, _offsetZoom);

                    for (double x = fullTileRange[z].MinX; x <= fullTileRange[z].MaxX; ++x)
                    {
                        for (double y = fullTileRange[z].MinY; y <= fullTileRange[z].MaxY; ++y)
                        {
                            var tile = new TileCoord(z, x, y);
                            //_blockingQueue.TryAdd(tile, 500);
                            _blockingQueue.Add(tile);
                        }
                    }
                }
            }
        }