Ejemplo n.º 1
0
        internal static StorageTask <WebResponse> GetResponseAsyncWithTimeout(this WebRequest req, CloudBlobClient service, TimeSpan?timeout)
        {
            StorageTask <WebResponse> serverTask = req.GetResponseAsyncEx(service);

            StorageTask <WebResponse> wrappedTask = TimeoutHelper.GetTimeoutWrappedTask(timeout, serverTask);

            return(wrappedTask);
        }
Ejemplo n.º 2
0
        public TaskAsyncResult(StorageTask <T> async, AsyncCallback callback, object state)
        {
            this.realTask     = async;
            this.userCallback = callback;
            this.userState    = state;

            // We don't need finalization, we just implement IDisposable to let users close the event
            GC.SuppressFinalize(this);

            this.realTask.ExecuteStep(this.OnComplete);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets a timeout wrapped task.
        /// </summary>
        /// <typeparam name="T">The type of the result.</typeparam>
        /// <param name="timeout">The timeout.</param>
        /// <param name="originalTask">The original task.</param>
        /// <returns>A <see cref="Task&lt;T&gt;"/> that has been wrapped with a timeout if specified.</returns>
        internal static StorageTask <T> GetTimeoutWrappedTask <T>(TimeSpan?timeout, StorageTask <T> originalTask)
        {
            TraceHelper.WriteLine("Creating timeout wrapped task " + timeout);

            if (timeout.HasValue)
            {
                StorageTask <T> wrappedTimeoutTask = new InvokeTaskSequenceTask <T>((setResult) =>
                                                                                    GetTimeoutTaskSequence(timeout.Value, setResult));
                return(new RaceTask <T>(wrappedTimeoutTask, originalTask));
            }
            else
            {
                return(originalTask);
            }
        }
Ejemplo n.º 4
0
        protected override void ExecuteInternal()
        {
            TraceHelper.WriteLine("RaceTask, ExecuteInternal");

            // kick off the tasks
            foreach (StorageTask <T> async in this.tasks)
            {
                if (this.Completed)
                {
                    break;
                }

                StorageTask <T> a2 = async;
                a2.ExecuteStep(delegate { this.OnComplete(a2); });
            }
        }
Ejemplo n.º 5
0
        internal static TaskSequence ReadToString(this Stream stream, System.Text.Encoding encoding, Action <string> result)
        {
            using (var ms = new SmallBlockMemoryStream())
            {
                int read = -1;
                while (read != 0)
                {
                    byte[]            buffer = new byte[1024];
                    StorageTask <int> count  = stream.ReadAsyncEx(buffer, 0, 1024);
                    yield return(count);

                    ms.Write(buffer, 0, count.Result);
                    read = count.Result;
                }

                ms.Seek(0, SeekOrigin.Begin);
                using (var strm = new StreamReader(ms, encoding))
                {
                    result(strm.ReadToEnd());
                }
            }
        }
Ejemplo n.º 6
0
        private void OnComplete(StorageTask <T> winner)
        {
            TraceHelper.WriteLine("RaceTask, OnComplete");
            if (this.Completed)
            {
                return;
                ////throw new InvalidOperationException("completion already occurred");
            }

            int currentlyAborting = Interlocked.CompareExchange(ref this.aborting, 1, 0);

            if (currentlyAborting == 1)
            {
                // the loser tasks (ie. this) are 'completing'
                return;
            }

            // winner reaches here
            // abort the other tasks
            foreach (StorageTask <T> loser in this.tasks)
            {
                if (!object.ReferenceEquals(loser, winner))
                {
                    loser.Abort();
                }
            }

            if (winner.Exception != null)
            {
                this.Exception = winner.Exception;
            }
            else
            {
                SetResult(() => winner.Result);
            }

            Complete(winner.CompletedSynchronously);
        }
Ejemplo n.º 7
0
 internal static IAsyncResult ToAsyncResult(this StorageTask <NullTaskReturn> async, AsyncCallback callback, object state)
 {
     return(new TaskAsyncResult <NullTaskReturn>(async, callback, state));
 }