示例#1
0
        void RunBackgroundThread()
        {
            while (!disposed)
            {
                // Wait for an image to be added or Dispose() to be called
                backgroundThreadEvent.Wait();

                AsyncRestCall asyncRestCall = null;
                lock (this.lockObject)
                {
                    if (RequestQueue.Count > 0)
                    {
                        asyncRestCall = RequestQueue.First.Value;
                        RequestQueue.RemoveFirst();
                    }
                    else
                    {
                        // Queue is empty, so resume waiting
                        backgroundThreadEvent.Reset();
                    }
                }

                if (asyncRestCall != null)
                {
                    asyncRestCall.ProcessThreadsafe();
                }
            }
        }
示例#2
0
        public T ProcessRequest <T>(YamsterHttpRequest request)
        {
            AsyncRestCall call = CreateRequestObject(request);

            call.ProcessThreadsafe();
            byte[] bytes = call.GetResult();
            return(YamsterApi.ParseJsonResponse <T>(bytes));
        }
示例#3
0
        // This is the part that is safe to execute outside the main thread
        internal void ProcessThreadsafe()
        {
            if (this.IsCompleted)
            {
                throw new InvalidOperationException("Process() was already called for this object");
            }

            try
            {
                DateTime startTime = DateTime.Now;

                using (HttpWebResponse response = (HttpWebResponse)this.HttpWebRequest.GetResponse())
                {
                    TimeSpan duration = DateTime.Now - startTime;

                    Debug.WriteLine("Status: {0}, received {1} bytes, from cache: {2:l}, {3:N2}ms",
                                    response.StatusCode, response.ContentLength, response.IsFromCache.ToString().ToLower(), duration.TotalMilliseconds);

                    using (var reader = new BinaryReader(response.GetResponseStream()))
                    {
                        this.ResultBytes = AsyncRestCall.ReadAllBytes(reader);
                    }
                }
            }
            catch (WebException ex)
            {
                try
                {
                    YamsterApi.CheckForErrors(ex);
                    this.ResultException = ex; // if Handle() didn't throw, then keep the original exception
                }
                catch (Exception ex2)
                {
                    this.ResultException = ex2;
                }
            }
            Debug.Assert(this.IsCompleted);

            // Signal that the operation is complete
            if (Semaphore != null)
            {
                Semaphore.Release();
            }
        }
示例#4
0
        public async Task <byte[]> ProcessRawRequestAsync(YamsterHttpRequest request)
        {
            AsyncRestCall asyncRestCall = CreateRequestObject(request);

            asyncRestCall.Semaphore = new SemaphoreSlim(initialCount: 0, maxCount: 1);
            this.EnsureBackgroundThreadStarted();
            lock (this.lockObject)
            {
                RequestQueue.AddLast(asyncRestCall);
                backgroundThreadEvent.Set();
            }

            await asyncRestCall.Semaphore.WaitAsync();

            asyncRestCall.Semaphore.Dispose();

            byte[] bytes = asyncRestCall.GetResult();
            return(bytes);
        }