public void BeginLoadTiles(MapTileLayer tileLayer, IEnumerable <Tile> tiles)
        {
            var imageTileSource = tileLayer.TileSource as ImageTileSource;

            foreach (var tile in tiles)
            {
                try
                {
                    ImageSource image = null;

                    if (imageTileSource != null)
                    {
                        image = imageTileSource.LoadImage(tile.XIndex, tile.Y, tile.ZoomLevel);
                    }
                    else
                    {
                        var uri = tileLayer.TileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);

                        if (uri != null)
                        {
                            image = new BitmapImage(uri);
                        }
                    }

                    tile.SetImage(image);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Loading tile image failed: {0}", ex.Message);
                }
            }
        }
        public void BeginLoadTiles(MapTileLayer tileLayer, IEnumerable <Tile> tiles)
        {
            if (tiles.Any())
            {
                // get current TileLayer property values in UI thread
                var dispatcher      = tileLayer.Dispatcher;
                var tileSource      = tileLayer.TileSource;
                var imageTileSource = tileSource as ImageTileSource;

                if (imageTileSource != null && !imageTileSource.IsAsync) // call LoadImage in UI thread with low priority
                {
                    foreach (var tile in tiles)
                    {
                        dispatcher.BeginInvoke(new Action <Tile>(t => t.SetImage(LoadImage(imageTileSource, t))), DispatcherPriority.Background, tile);
                    }
                }
                else
                {
                    var tileList     = tiles.ToList(); // evaluate immediately
                    var sourceName   = tileLayer.SourceName;
                    var maxDownloads = tileLayer.MaxParallelDownloads;

                    Task.Run(() => GetTiles(tileList, dispatcher, tileSource, sourceName, maxDownloads));
                }
            }
        }
        public void CancelLoadTiles(MapTileLayer tileLayer)
        {
            PendingTile pendingTile;

            while (pendingTiles.TryDequeue(out pendingTile))
            {
                ;                                              // no Clear method
            }
        }
Example #4
0
        public void BeginLoadTiles(MapTileLayer tileLayer, IEnumerable <Tile> tiles)
        {
            var tileSource      = tileLayer.TileSource;
            var imageTileSource = tileSource as ImageTileSource;

            if (imageTileSource != null)
            {
                foreach (var tile in tiles)
                {
                    try
                    {
                        tile.SetImage(imageTileSource.LoadImage(tile.XIndex, tile.Y, tile.ZoomLevel));
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }
            else
            {
                foreach (var tile in tiles)
                {
                    Uri uri = null;

                    try
                    {
                        uri = tileSource.GetUri(tile.XIndex, tile.Y, tile.ZoomLevel);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }

                    if (uri == null)
                    {
                        tile.SetImage(null);
                    }
                    else
                    {
                        pendingTiles.Enqueue(new PendingTile(tile, uri));

                        var newTaskCount = Math.Min(pendingTiles.Count, tileLayer.MaxParallelDownloads) - taskCount;
                        var sourceName   = tileLayer.SourceName;

                        while (newTaskCount-- > 0)
                        {
                            Interlocked.Increment(ref taskCount);

                            Task.Run(() => LoadPendingTiles(tileSource, sourceName)); // Task.Run(Func<Task>)
                        }
                    }
                }
            }
        }
        public async Task LoadTilesAsync(MapTileLayer tileLayer)
        {
            pendingTiles.Clear();

            var tileSource = tileLayer.TileSource;
            var sourceName = tileLayer.SourceName;
            var tiles      = tileLayer.Tiles.Where(t => t.Pending);

            if (tileSource != null && tiles.Any())
            {
                if (Cache == null || string.IsNullOrEmpty(sourceName) ||
                    tileSource.UriFormat == null || !tileSource.UriFormat.StartsWith("http"))
                {
                    // no caching, load tile images directly

                    foreach (var tile in tiles)
                    {
                        await LoadTileImageAsync(tileSource, tile);
                    }
                }
                else
                {
                    pendingTiles.PushRange(tiles.Reverse().ToArray());

                    while (taskCount < Math.Min(pendingTiles.Count, DefaultConnectionLimit))
                    {
                        Interlocked.Increment(ref taskCount);

                        var task = Task.Run(async() =>                           // do not await
                        {
                            await LoadPendingTilesAsync(tileSource, sourceName); // run multiple times in parallel

                            Interlocked.Decrement(ref taskCount);
                        });
                    }
                }
            }
        }
        /// <summary>
        /// Loads all pending tiles from the Tiles collection of a MapTileLayer by running up to MaxLoadTasks parallel Tasks.
        /// If the TileSource's SourceName is non-empty and its UriFormat starts with "http", tile images are cached in the
        /// TileImageLoader's Cache.
        /// </summary>
        public void LoadTilesAsync(MapTileLayer tileLayer)
        {
            pendingTiles.Clear();

            var tileSource = tileLayer.TileSource;
            var sourceName = tileLayer.SourceName;
            var tiles      = tileLayer.Tiles.Where(t => t.Pending);

            if (tileSource != null && tiles.Any())
            {
                pendingTiles.PushRange(tiles.Reverse().ToArray());

                Func <Tile, Task> loadFunc;

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

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

                while (--newTasks >= 0)
                {
                    Interlocked.Increment(ref taskCount);

                    var task = Task.Run(() => LoadTilesAsync(loadFunc)); // do not await
                }

                //Debug.WriteLine("{0}: {1} tasks", Environment.CurrentManagedThreadId, taskCount);
            }
        }
 public void CancelLoadTiles(MapTileLayer tileLayer)
 {
 }