New Posts New Posts RSS Feed: async and await
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

async and await

 Post Reply Post Reply
Author
GeorgeB View Drop Down
Groupie
Groupie


Joined: 03-May-2010
Posts: 66
Post Options Post Options   Quote GeorgeB Quote  Post ReplyReply Direct Link To This Post Topic: async and await
    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


Edited by GeorgeB - 28-Feb-2012 at 11:06am
Back to Top
GeorgeB View Drop Down
Groupie
Groupie


Joined: 03-May-2010
Posts: 66
Post Options Post Options   Quote GeorgeB Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
GeorgeB View Drop Down
Groupie
Groupie


Joined: 03-May-2010
Posts: 66
Post Options Post Options   Quote GeorgeB Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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();
}
Back to Top
GeorgeB View Drop Down
Groupie
Groupie


Joined: 03-May-2010
Posts: 66
Post Options Post Options   Quote GeorgeB Quote  Post ReplyReply Direct Link To This Post Posted: 29-Feb-2012 at 10:48am
No problem.
 
Keep up the good work with Cocktail.
Back to Top
mseeli View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Sep-2010
Location: Switzerland
Posts: 31
Post Options Post Options   Quote mseeli Quote  Post ReplyReply Direct Link To This Post 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;
        }
    }

Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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. 


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();
        }
    }
}
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down