Asynchronous Operations

Most SimolClient methods can be invoked asynchronously with just a few additional lines of code.

Asynchronous operations are implemented using extension methods that follow the standard Begin*/End* method syntax used throughout the .NET framework . To use the async extension methods you must import the Simol.Async namespace. The following example demonstrates how to retrieve an Employee asynchronously:

    using Simol.Async;
    // [snip]   
    var employeeId = new Guid("6c0a37b4-9c59-49ce-95af-d01a66f9605d");
    IAsyncResult result = simol.BeginGet<Employee>(employeeId, null, null);

    // do something else useful
    var e = simol.EndGet<Employee>(result);

The call to EndGet will throw any exceptions encountered during the asynchronous operation.

Storing data asynchronously works the same way:

    using Simol.Async;
    //[snip]       
    var employee = new Employee
	{
            Id = Guid.NewGuid(),
            Email = "jed@example.com"
    	};
    IAsyncResult result = simol.BeginPut(employee, null, null);

    // do something else useful
    simol.EndPut(result);

You can also provide an AsyncCallback delegate to be notified when asynchronous operations complete rather than blocking on the EndXXX methods:

    AsyncCallback callback = delegate(IAsyncResult result) { simol.EndPut(result); };

    simol.BeginPut(new Appointment(), callback, null);
    simol.BeginPut(new Customer(), callback, null);
    simol.BeginPut(new Employee(), callback, null);

    // do something else useful

Last edited May 24, 2011 at 3:31 AM by ashleytate, version 8

Comments

ashleytate Jun 19, 2010 at 1:41 PM 
If you want to fire off a batch of async actions and know when they're ALL complete you need to keep a list of the IAsyncResults returned from each method, loop through them at the end of your program, and call the corresponding EndXXX method for EACH result. The EndXXX calls will block for any incomplete async operations, so when your end loop is finished the program can terminate safely. If you're using distributed fire-and-forget with callback delegates you could keep a global counter of open async calls, decrement in the callback method, and delay program termination until the counter reaches 0.

(By the way, Codeplex doesn't have notifications for comments posted to these doc pages so you'll almost always get a faster response by posting to the discussion area.)

bdm Jun 15, 2010 at 3:25 AM 
Ashley,
This async stuff works great - except perhaps just as the program is exiting. Is there a mechanism to finish all asynchronous Puts to simpleDB? I didn't see a Dispose or anything I recognized that might fit the bill. Thanks again!