Await MatriX

C# 5.0 brought us the great new Async and Await feature. It helps to avoid bottlenecks when it comes to performance or to improve the responsiveness of UI applications.

Both of them were never a problem in MatriX, because its completely asynchronous internal. We helped GUI developers a lot by handling all multi threading and invoking to GUI threads inside of MatriX.

But the traditional techniques for writing asynchronous code based on callbacks can be complicated, difficult to maintain, hard to debug, error prone, and often ends in the callback hell.

Here is an for requesting a vcard with the new async await API

var viq = new VcardIq { To = "bob@ag-software.net", Type = IqType.get };
var vcardResult = await xmppClient.IqFilter.SendIqAsync(viq);

Here is a more complex example of creating a MUC room containing the following steps.

  • request our reserved nickname
  • create the room with the requested nickname
  • request the room configuration
  • submit the room configuration

With the old async callback based API this simple task requires you to setup multiple callbacks or listen to multiple events. This splits your code in many different functions, or you use many inline lambdas. Error handling gets complicated and the control flow is hard to follow.

So look at the code with the new async await API. It looks like simple synchronous code. Easy to read with simple try/catch error handling, fully asynchronous. The compiler is doing all the complicated work for us.

try
{
    Jid roomJid = "dev@conference.ag-software.de";
    var nick = await mucManager.DiscoverReservedNicknameAsync(roomJid);
    var createResult = await mucManager.CreateRoomAsync(roomJid, nick);
    // check from createResult if the room requires configuration
    if (createResult.Error != null 
        && createResult.Error.Condition == ErrorCondition.ItemNotFound)
    {
        var roomConfig = await mucManager.RequestRoomConfigurationAsync(roomJid);
        var data = ProcessRoomConfig(roomConfig);
        var submitResult = await mucManager.SubmitRoomConfigurationAsync(roomJid, data);  
    }        
}
catch (TimeoutException ex)
{
     // handle exceptions here
}
catch (Exception ex)
{
     // handle exceptions here
}

Many of our customers are tied to older .NET versions and not able to use the latest and greatest features. This is the reason why we will offer separate builds for .NET 4.5 with the async API extensions, and .NET 3.5 builds.

We will upload new .NET 4.5 builds soon, so stay tuned. If you can’t await then contact us directly ;-)

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.