Ejemplo n.º 1
0
        private ProgressReportingTask <TResult, TProgress> GetAsynchronousResponseWithProgress <TResult, TProgress>(AsyncResults taskCompletionSource, CancellationToken cancellationToken)
        {
            ProgressReportingTask <TResult, TProgress> progressReportingTask = new ProgressReportingTask <TResult, TProgress>(
                progress =>
            {
                object obj;
                while (taskCompletionSource.TryTake(out obj, (int)TimeSpan.FromMinutes(2).TotalMilliseconds, cancellationToken))
                {
                    progress.MakeProgress((TProgress)obj);
                    if (obj is TProgress)
                    {
                        progress.MakeProgress((TProgress)obj);
                    }
                    else if (obj is TResult)
                    {
                        return((TResult)obj);
                    }
                    else
                    {
                        throw new ArgumentException("Cannot process result type of " + taskCompletionSource.GetConsumingEnumerable(cancellationToken).First().GetType());
                    }
                }
                throw new AbandonedMutexException("We did not receive of reply from the server after 2 minutes for transaction " + taskCompletionSource.Id);
            }, cancellationToken, TaskCreationOptions.None);

            progressReportingTask.Start(BackgroundTaskScheduler.OnBackground());
            Task tt = progressReportingTask;

            tt.OnCancelled(
                canceled =>
            {
                Trace.WriteLine("Got some more cancels");
            }, UITaskScheduler.InvokeAsync());                             // bug here - not being called on cancel
            return(progressReportingTask);
        }
Ejemplo n.º 2
0
        private TResponse ExecServerRequest <TResponse, TRequest>(TRequest request,
                                                                  SvcGatewayDelegate <TRequest, TResponse> svcGatewayMethod, TimeSpan timeout, AsyncResults asyncResults)
            where TResponse : ServiceResponse
            where TRequest : ICommonRequest
        {
            TResponse response = svcGatewayMethod(request);

            if (response.ResponseCode != ResponseCode.Succeeded)
            {
                return(response);
            }
            object serviceResponse;

            if (asyncResults.TryTake(out serviceResponse, timeout))
            {
                if (serviceResponse is IFaulted && ((IFaulted)serviceResponse).IsFaulted)
                {
                    throw new AggregateException(((IFaulted)serviceResponse).Message);
                }
                response = (TResponse)serviceResponse;
                if (response != null && response.ResponseCode != ResponseCode.Succeeded &&
                    !string.IsNullOrWhiteSpace(response.ErrorMessage))
                {
                    throw new ServerException(response.ErrorMessage);
                }
                return(response);
            }
            string timeOutString = TimeSpan.FromMinutes(2) > timeout ? timeout.TotalSeconds + "seconds" : "2 minutes";

            throw new TimeoutException("We did not receive of reply from the server after " + timeOutString +
                                       " for transaction " + asyncResults.Id);
        }
Ejemplo n.º 3
0
 private Task <TResult> GetAsynchronousResponse <TResult>(AsyncResults taskCompletionSource, CancellationToken cancellationToken)
 {
     return(TaskFactoryHelper <TResult> .StartNew(
                () =>
     {
         object obj;
         while (!(taskCompletionSource.TryTake(out obj, (int)TimeSpan.FromMinutes(2).TotalMilliseconds, cancellationToken)))
         {
             if (obj is TResult)
             {
                 return (TResult)obj;
             }
         }
         throw new AbandonedMutexException("We did not receive of reply from the server after 2 minutes for transaction " + taskCompletionSource.Id);
     }, BackgroundTaskScheduler.OnBackground(), cancellationToken));
 }