// 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(); } }
internal async Task SetMessageLikeStatusAsync(long messageId, bool liked) { // TODO: Need to schedule this request rather than executing it immediately TallyRequest(); var parameters = new NameValueCollection(); parameters["message_id"] = messageId.ToString(); try { if (liked) { await this.asyncRestCaller.PostFormAsync("/api/v1/messages/liked_by/current.json", parameters, YamsterHttpMethod.Post); } else { parameters["_method"] = "DELETE"; await this.asyncRestCaller.PostFormAsync("/api/v1/messages/liked_by/current.json", parameters, YamsterHttpMethod.Delete); } } catch (WebException ex) { try { YamsterApi.CheckForErrors(ex); } catch (RateLimitExceededException) { NotifyRateLimitExceeded(); throw; } throw; } }