public static Task <T> EnqueueAsync <T>(TwitterAccount account, RequestBase <T> request) { return(account.GetTaskFactory().StartNew(async() => { var retryCount = 0; do { try { return await request.Send(account).ConfigureAwait(false); } catch (WebException ex) { if (!CheckTemporaryError(ex) || retryCount >= request.RetryCount) { // request is something wrong. throw; } } catch (Exception) { if (retryCount >= request.RetryCount) { // request is something wrong. throw; } } retryCount++; // wait retry delay await Task.Delay(TimeSpan.FromSeconds(request.RetryDelaySec)).ConfigureAwait(false); } while (true); }).Unwrap()); }
public static IObservable <T> Enqueue <T>(TwitterAccount account, RequestBase <T> request) { #pragma warning disable 4014 var subject = new Subject <T>(); account.GetTaskFactory().StartNew(async() => { Exception thrown; var retryCount = 0; do { try { var result = await request.Send(account); Task.Run(() => { subject.OnNext(result); subject.OnCompleted(); }); return; } catch (WebException ex) { thrown = ex; if (!CheckTemporaryError(ex)) { // request is something wrong. break; } } catch (Exception ex) { thrown = ex; } retryCount++; if (retryCount < request.RetryCount) { // wait retry delay await Task.Run(() => Thread.Sleep(TimeSpan.FromSeconds(request.RetryDelaySec))); } } while (retryCount < request.RetryCount); // throw last exception Task.Run(() => subject.OnError(thrown)); }); return(subject.AsObservable()); #pragma warning restore 4014 }