private void LoadMap() { if (MapListBox.SelectedValue == null) { return; } var basedir = ConfigurationManager.AppSettings["basedir"]; string _SelectedMapName = ((KeyValuePair <int, string>)MapListBox.SelectedValue).Value; WDTGrid.Children.Clear(); pbLoadMap.Value = 0d; var wdt = new WDTReader(basedir); if (File.Exists(System.IO.Path.Combine(basedir, "World\\Maps\\", _SelectedMapName, _SelectedMapName + ".wdt"))) { Stopwatch _SW = new Stopwatch(); BackgroundWorker _BackgroundWorker = new BackgroundWorker(); _BackgroundWorker.WorkerReportsProgress = true; _BackgroundWorker.DoWork += new DoWorkEventHandler( (object o, DoWorkEventArgs args) => { _SW.Start(); BackgroundWorker _Worker = o as BackgroundWorker; wdt.LoadWDT(System.IO.Path.Combine("World\\Maps\\", _SelectedMapName, _SelectedMapName + ".wdt")); List <int[]> tiles = wdt.getTiles(); for (int i = 0; i < tiles.Count; i++) { if (fCancelMapLoading) { break; } Action _LoadTileAction = delegate() { LoadTile(basedir, tiles[i]); }; this.Dispatcher.Invoke(_LoadTileAction); _Worker.ReportProgress((i * 100) / tiles.Count); } _Worker.ReportProgress(100); }); _BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler( (object o, ProgressChangedEventArgs args) => { pbLoadMap.Value = args.ProgressPercentage; }); _BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( (object sender, RunWorkerCompletedEventArgs args) => { fCancelMapLoading = false; _SW.Stop(); Console.WriteLine("Loading {0} took {1} seconds", _SelectedMapName, _SW.Elapsed.TotalSeconds, _SW.ElapsedMilliseconds); }); _BackgroundWorker.RunWorkerAsync(); } }
private void LoadMap() { if (MapListBox.SelectedValue == null) { return; } string _SelectedMapName = ((KeyValuePair <int, string>)MapListBox.SelectedValue).Value; WDTGrid.Children.Clear(); progressBar.Visibility = Visibility.Visible; progressLabel.Visibility = Visibility.Visible; progressBar.Value = 0; progressLabel.Content = "Loading minimap.."; var wdt = new WDTReader(); if (CASC.FileExists(System.IO.Path.Combine(@"world\maps\", _SelectedMapName, _SelectedMapName + ".wdt"))) { Stopwatch _SW = new Stopwatch(); BackgroundWorker _BackgroundWorker = new BackgroundWorker(); _BackgroundWorker.WorkerReportsProgress = true; _BackgroundWorker.DoWork += new DoWorkEventHandler( (object o, DoWorkEventArgs args) => { _SW.Start(); BackgroundWorker _Worker = o as BackgroundWorker; wdt.LoadWDT(System.IO.Path.Combine(@"world\maps\", _SelectedMapName, _SelectedMapName + ".wdt")); List <int[]> tiles = wdt.getTiles(); for (int i = 0; i < tiles.Count; i++) { if (fCancelMapLoading) { break; } Action _LoadTileAction = delegate() { LoadTile(tiles[i]); }; this.Dispatcher.Invoke(_LoadTileAction); _Worker.ReportProgress((i * 100) / tiles.Count, "Loading minimap.."); } _Worker.ReportProgress(100, "Minimap loaded."); }); _BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler( (object o, ProgressChangedEventArgs args) => { progressBar.Value = args.ProgressPercentage; progressLabel.Content = (string)args.UserState; }); _BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( (object sender, RunWorkerCompletedEventArgs args) => { fCancelMapLoading = false; _SW.Stop(); }); _BackgroundWorker.RunWorkerAsync(); } }