// Private methods. /// <summary> /// Creates an asynchronous state, and executes the method on the thread pool. /// </summary> /// <param name="method">The method to execute.</param> /// <param name="userState">The user state.</param> private void Execute(AsyncTaskCallback method, object userState) { // Create the asynchronous state for the current method. AsyncState asyncState = new AsyncState(userState); // Execute the method on the thread pool. ThreadPool.QueueUserWorkItem((object state) => { // Lock the task. lock (this.syncTask) { // Lock the state. lock (this.syncState) { // Change the task state. this.taskState = AsyncTaskState.Running; // Set the current asychronous state. this.asyncState = asyncState; } // Execute the task method. method(asyncState); // If the method has been canceled. if (asyncState.IsCanceled) { // Execute the cancel callback method. asyncState.CancelCallback(); } // Lock the state. lock (this.syncState) { // Change the task state. this.taskState = AsyncTaskState.Ready; // Set the current asychronous state to null. this.asyncState = null; // Signal the completion of the asynchronous operation. asyncState.Complete(); } // Dispose of the asynchronous state. asyncState.Dispose(); } }); }
/// <summary> /// Creates a new bitmap for the current map, scaled at the specified size. The scaled version of the map fits the largest dimension between width and height. /// </summary> /// <param name="asyncState">The asynchronous state.</param> /// <returns>The map bitmap.</returns> private Bitmap OnDrawMap(AsyncState asyncState) { // Save the map object in a local variable. Map map = this.map; // Acquire an exclusive access to the map. lock (map) { // Create a new bitmap. Bitmap bitmap = new Bitmap(bitmapSize.Width, bitmapSize.Height); // Draw the bitmap. using (Graphics graphics = Graphics.FromImage(bitmap)) { // Set the smooting mode to default. graphics.SmoothingMode = SmoothingMode.Default; using (SolidBrush brush = new SolidBrush(this.colorBackground)) { using (Pen pen = new Pen(this.colorRegionNormalBorder)) { // Fill the background. graphics.FillRectangle(brush, 0, 0, bitmapSize.Width, bitmapSize.Height); // Set the smooting mode to high quality. graphics.SmoothingMode = SmoothingMode.HighQuality; // Change the brush color. brush.Color = this.colorRegionNormalBackground; // Get an exclusive reader lock to the regions list. this.regions.Lock(); try { // Draw the map regions. foreach (MapRegion region in this.regions) { // If the asynchronous operation has been canceled. if (asyncState.IsCanceled) { // Dispose the bitmap. bitmap.Dispose(); // Return null. return null; } // Draw the region. region.Draw(graphics, brush, this.showBorders ? pen : null); } } finally { this.regions.Unlock(); } } } } // Return the bitmap. return bitmap; } }