Example #1
0
        /// <summary>
        /// Loads all pending tiles from the tiles collection.
        /// If tileSource.UriFormat starts with "http" and sourceName is a non-empty string,
        /// tile images will be cached in the TileImageLoader's Cache (if it's not null).
        /// The method is async void because it implements void ITileImageLoader.LoadTilesAsync
        /// and is not awaited when it is called in MapTileLayer.UpdateTiles().
        /// </summary>
        public async void LoadTilesAsync(IEnumerable <Tile> tiles, TileSource tileSource, string sourceName)
        {
            tileQueue.Clear();

            tiles = tiles.Where(tile => tile.Pending);

            if (tiles.Any() && tileSource != null)
            {
                if (Cache != null &&
                    tileSource.UriFormat != null &&
                    tileSource.UriFormat.StartsWith("http") &&
                    !string.IsNullOrEmpty(sourceName))
                {
                    loadTileImage = tile => LoadCachedTileImageAsync(tile, tileSource, sourceName);
                }
                else
                {
                    loadTileImage = tile => LoadTileImageAsync(tile, tileSource);
                }

                tileQueue.Enqueue(tiles);

                var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;

                if (newTasks > 0)
                {
                    Interlocked.Add(ref taskCount, newTasks);

                    await Task.WhenAll(Enumerable.Range(0, newTasks).Select(n => LoadTilesFromQueueAsync())).ConfigureAwait(false);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks.
        /// If the UriFormat of the TileSource starts with "http" and the sourceName string is non-empty,
        /// tile images are cached in the TileImageLoader's Cache.
        /// </summary>
        public void LoadTilesAsync(IEnumerable <Tile> tiles, TileSource tileSource, string sourceName)
        {
            tileQueue.Clear();

            if (tileSource != null)
            {
                tileQueue.Enqueue(tiles);

                var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;

                if (newTasks > 0)
                {
                    Func <Tile, Task> loadTileFunc;

                    if (Cache != null &&
                        tileSource.UriFormat != null &&
                        tileSource.UriFormat.StartsWith("http") &&
                        !string.IsNullOrEmpty(sourceName))
                    {
                        loadTileFunc = tile => LoadCachedTileImageAsync(tile, tileSource, sourceName);
                    }
                    else
                    {
                        loadTileFunc = tile => LoadTileImageAsync(tile, tileSource);
                    }

                    Interlocked.Add(ref taskCount, newTasks);

                    while (--newTasks >= 0)
                    {
                        Task.Run(() => LoadTilesFromQueueAsync(loadTileFunc));
                    }
                }
            }
        }
        /// <summary>
        /// Loads all pending tiles from the tiles collection.
        /// If tileSource.UriFormat starts with "http" and cacheName is a non-empty string,
        /// tile images will be cached in the TileImageLoader's Cache (if that is not null).
        /// </summary>
        public void LoadTiles(IEnumerable <Tile> tiles, TileSource tileSource, string cacheName)
        {
            tileQueue.Clear();

            tiles = tiles.Where(tile => tile.Pending);

            if (tiles.Any() && tileSource != null)
            {
                if (Cache != null &&
                    tileSource.UriFormat != null &&
                    tileSource.UriFormat.StartsWith("http") &&
                    !string.IsNullOrEmpty(cacheName))
                {
                    loadTileFunc = tile => LoadCachedTileAsync(tile, tileSource, cacheName);
                }
                else
                {
                    loadTileFunc = tile => LoadTileAsync(tile, tileSource);
                }

                tileQueue.Enqueue(tiles);

                while (taskCount < Math.Min(tileQueue.Count, MaxLoadTasks))
                {
                    Interlocked.Increment(ref taskCount);

                    Task.Run(LoadTilesFromQueueAsync);
                }
            }
        }
        /// <summary>
        /// Loads all pending tiles from the tiles collection in up to MaxLoadTasks parallel Tasks.
        /// If the UriFormat of TileSource starts with "http" and SourceName is a non-empty string,
        /// tile images will be cached in the TileImageLoader's Cache.
        /// </summary>
        public void LoadTilesAsync(IEnumerable <Tile> tiles)
        {
            tileQueue.Clear();
            tileQueue.Enqueue(tiles);

            var newTasks = Math.Min(tileQueue.Count, MaxLoadTasks) - taskCount;

            if (newTasks > 0)
            {
                Interlocked.Add(ref taskCount, newTasks);

                while (--newTasks >= 0)
                {
                    Task.Run(() => LoadTilesFromQueueAsync());
                }
            }
        }