Example #1
0
        public static System.Threading.Tasks.Task LoginAsync(string username, string password)
        {
            var tcs = new System.Threading.Tasks.TaskCompletionSource <PTPrincipal>();

            PTIdentity.GetPTIdentity(username, password, (o, e) =>
            {
                if (e.Error == null && e.Object != null)
                {
                    SetPrincipal(e.Object);
                    tcs.SetResult(null);
                }
                else
                {
                    Logout();
                    if (e.Error != null)
                    {
                        tcs.SetException(e.Error.InnerException);
                    }
                    else
                    {
                        tcs.SetCanceled();
                    }
                }
            });
            return(tcs.Task);
        }
Example #2
0
        public static System.Threading.Tasks.Task <eina.Value> WrapAsync(eina.Future future, CancellationToken token)
        {
            // Creates a task that will wait for SetResult for completion.
            // TaskCompletionSource is used to create tasks for 'external' Task sources.
            var tcs = new System.Threading.Tasks.TaskCompletionSource <eina.Value>();

            // Flag to be passed to the cancell callback
            bool fulfilled = false;

            future.Then((eina.Value received) => {
                    lock (future)
                    {
                        // Convert an failed Future to a failed Task.
                        if (received.GetValueType() == eina.ValueType.Error)
                        {
                            eina.Error err;
                            received.Get(out err);
                            if (err == eina.Error.ECANCELED)
                            {
                                tcs.SetCanceled();
                            }
                            else
                            {
                                tcs.TrySetException(new efl.FutureException(received));
                            }
                        }
                        else
                        {
                            // Will mark the returned task below as completed.
                            tcs.SetResult(received);
                        }
                        fulfilled = true;
                        return(received);
                    }
                });
            // Callback to be called when the token is cancelled.
            token.Register(() => {
                    lock (future)
                    {
                        // Will trigger the Then callback above with an eina.Error
                        if (!fulfilled)
                        {
                            future.Cancel();
                        }
                    }
                });

            return(tcs.Task);
        }
Example #3
0
    public static System.Threading.Tasks.Task LoginAsync(string username, string password)
    {
        var tcs = new System.Threading.Tasks.TaskCompletionSource<PTPrincipal>();

        PTIdentity.GetPTIdentity(username, password, (o, e) =>
        {
            if (e.Error == null && e.Object != null)
            {
                SetPrincipal(e.Object);
                tcs.SetResult(null);
            }
            else
            {
                Logout();
                if (e.Error != null) 
                    tcs.SetException(e.Error.InnerException);
                else
                    tcs.SetCanceled();
            }
        });
        return tcs.Task;
    }