Print Page | Close Window

async and await

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3305
Printed Date: 16-Apr-2024 at 5:27am


Topic: async and await
Posted By: GeorgeB
Subject: async and await
Date Posted: 28-Feb-2012 at 10:56am
Hi
For a number of reasons (too many to explain here) I would like to use async and await from the Async CTP in my Silverlight app.
Any suggestions on how my (Cocktail) Repository might define the Manager.SaveChangesAsync() ?
Tried this but EntitySaveOperation isn't 'awaitable'
public async Task<EntitySaveOperation> SaveAsync(Action<IEnumerable> onSuccess, Action<Exception> onFail)
{ return await Manager.SaveChangesAsync(); }
Thanks
George



Replies:
Posted By: GeorgeB
Date Posted: 28-Feb-2012 at 11:17am
Hi
Okay just in case someone asks:-
I have a 3 step process which has to save a record, then if that is successful, save a record to another table, and if that is successful, save a another record to a third table.
This is running inside a foreach loop where I might make 20 iterations.
Thanks
George


Posted By: mgood
Date Posted: 28-Feb-2012 at 1:32pm
Neither Cocktail nor DevForce supports Task and async/await yet. It's still CTP. What's wrong with Coroutines? They do everything async/await does without the syntax sugar.


Posted By: GeorgeB
Date Posted: 29-Feb-2012 at 10:38am
Just to be difficult? No seriously - we've used async / wait as much as we can in our app.
 
I'm using coroutines but I just want keep everything standard.
 
Now that we have a go-live beta of .Net 4.5, will we see anything soon?
 
Kr
George


Posted By: mgood
Date Posted: 29-Feb-2012 at 10:43am
No committment to supporting it before the official release of .NET 4.5, but we will support it.


Posted By: mgood
Date Posted: 29-Feb-2012 at 10:47am
Oh btw, in the meantime, there's nothing stopping you from writing a wrapper that wraps a DevForce asynchronous operation as a Task, so you could do the following. Take a look at AsOperationResult for how Cocktail wraps a DevForce operation as an OperationResult<T>, which implements IResult in order to be consumable from a Caliburn Coroutine. Wrapping it as a Task is not much different.
 
public async Task<EntitySaveOperation> SaveAsync(Action<IEnumerable> onSuccess, Action<Exception> onFail)
{
     return await Manager.SaveChangesAsync().AsTask();
}


Posted By: GeorgeB
Date Posted: 29-Feb-2012 at 10:48am
No problem.
 
Keep up the good work with Cocktail.


Posted By: mseeli
Date Posted: 29-Feb-2012 at 11:29pm
I have been using the async CTP in my project for month now. It works just fine. 
Here my wrapper around DevForce query.ExecuteAsync:

    public static class VisExtensions
    {
        static readonly ILog Log = LogManager.GetLog(typeof(VisExtensions));
 
        public static Task<List<T>> VisExecuteAsync<T>(this IEntityQuery<T> query) where T : Entity
        {
            var tcs = new TaskCompletionSource<List<T>>();
            
            query.ExecuteAsync(op =>
            {
                if (op.Error != null)
                {
                    Exception ex2 = new Exception("VisExtensions.VisExecuteAsync.Query.ExecuteAsync: Exception", op.Error);
                    Log.Error(ex2);
                    tcs.TrySetException(op.Error);
                }
                else
                {
                    List<T> resultList = new List<T>();
                    foreach (var r in op.Results)
                    {
                        resultList.Add(r);
                    }
                    tcs.TrySetResult(resultList);
                }
            });
           return tcs.Task;
        }
    }



Posted By: mgood
Date Posted: 07-Apr-2012 at 11:00am
Just resurrecting this thread. The latest Cocktail source code has now support for the Task-based Asynchronous Pattern in WPF and SL5. 

http://cocktail.codeplex.com/SourceControl/list/changesets - http://cocktail.codeplex.com/SourceControl/list/changesets  

This will be part of the next release.

If you are using the Async CTP, add the following class to your project in order to make all Cocktail asynchronous operations awaitable:

using System.Runtime.CompilerServices;

namespace Cocktail
{
    public static class AwaitFns
    {
        public static TaskAwaiter<T> GetAwaiter<T>(this OperationResult<T> operation)
        {
            return operation.AsTask().GetAwaiter();
        }

        public static TaskAwaiter GetAwaiter(this OperationResult operation)
        {
            return operation.AsTask().GetAwaiter();
        }

        public static TaskAwaiter<T> GetAwaiter<T>(this DialogOperationResult<T> operation)
        {
            return operation.AsTask().GetAwaiter();
        }
    }
}



Print Page | Close Window