Table of Contents

How to work with Asynchronous Methods in MonoGame

This topic describes how you can work with asynchronous methods in MonoGame.

Overview

This topic describes how you can work with asynchronous methods in MonoGame.

MonoGame provides many methods that operate asynchronously for operations that may take longer than the desired render-cycle length.

Asynchronous methods consist of four elements:

  • A Begin call that begins the asynchronous process. Begin methods return an IASyncResult object that can be used to poll for completion if a callback function is not used to detect the completion of the operation.
  • An End call that ends the asynchronous process and returns objects or data requested by the Begin call. Calling the corresponding End method for each Begin method is important to prevent deadlocks and other undesirable behavior.
  • An optional callback method that is called by the system when the asynchronous operation completes. This is passed to the Begin call.
  • An optional, arbitrary tracking object that can be supplied to Begin to uniquely identify a particular asynchronous request. This object is part of the IASyncResult returned by Begin, and is also present in the callback method's IASyncResult parameter. Because of this, it also can be used to pass arbitrary data to the callback method when the asynchronous process completes.

The two most common methods of working with asynchronous methods are to check for completion by polling or by callback. This topic describes both methods.

Note

For more exhaustive information about Event Based asynchronous methods, see Asynchronous Programming Design Patterns on MSDN.

Alternatively, review the alternative Task-Based Asynchronous patter approach.

To poll for asynchronous method completion

  1. Call the asynchronous Begin method, and save the returned IASyncResult object to a variable that will be checked for completion.

  2. In your update code, check IsCompleted.

  3. When IsCompleted is true, call the End method that corresponds to the Begin method called in step 1.

To use a callback to check for asynchronous method completion

  1. Call the asynchronous Begin method, passing it an AsyncCallback method that will be called when the asynchronous process is completed.

    AsyncCallback methods must return void, and take a single parameter: IASyncResult.

  2. In the callback, call the End method that corresponds to the Begin method called in step 1.

    The End method typically returns any data or objects requested by the Begin call.

See Also

© 2012 Microsoft Corporation. All rights reserved.