Exemple #1
0
        /// <summary>
        /// Convert a promise to a .Net 4.0 Task for use with the <c>async</c>/<c>await</c> keywords
        /// </summary>
        public static Task <T> ToTask <T>(this IPromise <T> promise, CancellationToken ct = default(CancellationToken))
        {
            var tcs = new TaskCompletionSource <T>();

            promise.Done(v =>
            {
                try
                {
                    tcs.TrySetResult(v);
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            }).Fail(ex =>
            {
                var cancel = ex as OperationCanceledException;
                if (cancel == null)
                {
                    tcs.TrySetException(ex);
                }
                else
                {
                    tcs.TrySetCanceled();
                }
            });

            if (ct != default(CancellationToken))
            {
                ct.Register(() =>
                {
                    promise.Cancel();
                });
            }

            return(tcs.Task);
        }
Exemple #2
0
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (_currentRun == null)
                {
                    if (_conn == null)
                    {
                        outputEditor.Text = "Please select a connection first";
                        return;
                    }

                    btnSubmit.Text    = "► Cancel";
                    outputEditor.Text = "Processing...";

                    _currentRun = _conn
                                  .Continue(c => _script.Execute(c))
                                  .UiPromise(this)
                                  .Done(s =>
                    {
                        outputEditor.Text = s;
                        _currentRun       = null;
                    })
                                  .Fail(ex => outputEditor.Text = ex.ToString());
                }
                else
                {
                    btnSubmit.Text = "► Run";
                    _currentRun.Cancel();
                    _currentRun = null;
                }
            }
            catch (Exception ex)
            {
                outputEditor.Text = ex.ToString();
            }
        }
Exemple #3
0
 public bool Cancel()
 {
     return(_promise.Cancel());
 }