Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
AnswerRe: how to create three moving cars from different directions meeting at the junction Pin
Pete O'Hanlon14-Jun-16 6:23
mvePete O'Hanlon14-Jun-16 6:23 
AnswerRe: how to create three moving cars from different directions meeting at the junction Pin
Matthew Hazlett15-Jun-16 9:58
Matthew Hazlett15-Jun-16 9:58 
QuestionError in listing open windows Pin
srikrishnathanthri13-Jun-16 20:08
srikrishnathanthri13-Jun-16 20:08 
AnswerRe: Error in listing open windows Pin
Richard MacCutchan13-Jun-16 21:03
mveRichard MacCutchan13-Jun-16 21:03 
GeneralRe: Error in listing open windows Pin
srikrishnathanthri13-Jun-16 23:49
srikrishnathanthri13-Jun-16 23:49 
GeneralRe: Error in listing open windows Pin
Pete O'Hanlon14-Jun-16 0:37
mvePete O'Hanlon14-Jun-16 0:37 
AnswerRe: Error in listing open windows Pin
KumarArunR10-May-17 20:03
KumarArunR10-May-17 20:03 
QuestionRX - Creating a Retry with a timer, and catch the exceptions Pin
Kenneth Haugland13-Jun-16 8:45
mvaKenneth Haugland13-Jun-16 8:45 
I have created a Retry function (using Rx with Rx-main and Rx-xaml packages from NuGet) that uses a timer to Retry the function if an error appears, but if it is not successful within a given timeframe, the presses is aborted.

I use the functiuon like shown below:
C#
    textBox.Text = "";

    Thread.CurrentThread.Name = "UI Thread";

    IScheduler thread1 = new NewThreadScheduler(x => new Thread(x) { Name = "Thread1" });

    TimeSpan RetryMaxTime = new TimeSpan(0, 0, 0,0,5);
    Observable.Create<int>(o =>
    {
        Console.WriteLine("Created on " + Thread.CurrentThread.Name);
        o.OnNext(1);
        o.OnError(new Exception("Something random just happened"));
        o.OnCompleted();
        return Disposable.Create(() => { });
    })
    .SubscribeOn(thread1)
    .Retry(RetryMaxTime)
    .ObserveOnDispatcher()
    .Subscribe(
        x =>
        {
            textBox.Text += x.ToString() + Environment.NewLine;
        }
        , ex => textBox.Text += ex.Message
        , () => { textBox.Text += "Completed on " + Thread.CurrentThread.Name; }
        );
}

Basically, I used the WeakSubscription code to create the function:
C#
public static class MyExtensions
{

    public static IObservable<TItem> Retry<TItem>(this IObservable<TItem> collection,TimeSpan ExceptionTimeout)
    {
        return Observable.Create<TItem>(obs =>
        {
            var weakSubscription = new WeakSubscription<TItem>(collection, obs, ExceptionTimeout);
            return () =>
            {
                weakSubscription.Dispose();
            };
        });
    }

    public class WeakSubscription<T> : IDisposable, IObserver<T>
    {
        private readonly WeakReference reference;
        private readonly IDisposable subscription;
        private Exception LastError;
        private bool EndRetry = false;
        private bool disposed;

      private System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

        public WeakSubscription(IObservable<T> observable, IObserver<T> observer, TimeSpan ExceptionTimeout)
        {
            this.reference = new WeakReference(observer);

            var interval = Observable.Interval(ExceptionTimeout);
            interval.SubscribeOn(Scheduler.CurrentThread).Subscribe(x =>
            EndRetry = true);
            this.subscription = observable.Retry()//.Subscribe(this);
                .Catch<T>(e=>e)
                //{
                    //LastError = error;
                  //  return Observable.Return("test");
                  //  })
            .Subscribe(this);

        }

        IObservable<T> ExceptionHappened(Exception ex)
        {
            return null;
        }

        void IObserver<T>.OnCompleted()
        {
            var observer = (IObserver<T>)this.reference.Target;
            if (observer != null)
                observer.OnCompleted();

            if (this.EndRetry)
                this.Dispose();

        }

        void IObserver<T>.OnError(Exception error)
        {
            var observer = (IObserver<T>)this.reference.Target;
            LastError = error;

            if (observer != null)
                observer.OnError(error);
            if (this.EndRetry)
                this.Dispose();
        }

        void IObserver<T>.OnNext(T value)
        {
            var observer = (IObserver<T>)this.reference.Target;

            if (observer != null)
                observer.OnNext(value);
            if (this.EndRetry)
                this.Dispose();

        }

        public void Dispose()
        {
            if (!this.disposed)
            {
                this.disposed = true;
                this.subscription.Dispose();
            }
        }
    }

}


As you can see, if the operation is unsuccessful after the tries, I'd like to get the Exception that was thrown inside the Retry function. My question is simply, how can I get the error thrown, or is there a better way of doing this altogether?
QuestionSQL Server 2005 query that uses the IIF with style date bool ? Pin
Member 245846711-Jun-16 22:44
Member 245846711-Jun-16 22:44 
AnswerRe: SQL Server 2005 query that uses the IIF with style date bool ? Pin
OriginalGriff11-Jun-16 23:14
mveOriginalGriff11-Jun-16 23:14 
GeneralRe: SQL Server 2005 query that uses the IIF with style date bool ? Pin
Member 245846712-Jun-16 17:08
Member 245846712-Jun-16 17:08 
GeneralRe: SQL Server 2005 query that uses the IIF with style date bool ? Pin
OriginalGriff12-Jun-16 20:23
mveOriginalGriff12-Jun-16 20:23 
QuestionMouse click poisition vary after stretching the image c# Pin
Shithun NK10-Jun-16 23:09
Shithun NK10-Jun-16 23:09 
AnswerRe: Mouse click poisition vary after stretching the image c# Pin
OriginalGriff11-Jun-16 0:48
mveOriginalGriff11-Jun-16 0:48 
QuestionHow to know all windows are in minimized state Pin
srikrishnathanthri10-Jun-16 19:15
srikrishnathanthri10-Jun-16 19:15 
AnswerRe: How to know all windows are in minimized state Pin
OriginalGriff10-Jun-16 20:48
mveOriginalGriff10-Jun-16 20:48 
GeneralRe: How to know all windows are in minimized state Pin
srikrishnathanthri11-Jun-16 1:18
srikrishnathanthri11-Jun-16 1:18 
GeneralRe: How to know all windows are in minimized state Pin
OriginalGriff11-Jun-16 2:03
mveOriginalGriff11-Jun-16 2:03 
QuestionProblemas Na Conexão Win Forms Depois de Instalado Pin
Maylkon9-Jun-16 2:15
Maylkon9-Jun-16 2:15 
AnswerRe: Problemas Na Conexão Win Forms Depois de Instalado Pin
Chris Quinn9-Jun-16 2:28
Chris Quinn9-Jun-16 2:28 
AnswerRe: Problemas Na Conexão Win Forms Depois de Instalado Pin
OriginalGriff9-Jun-16 2:42
mveOriginalGriff9-Jun-16 2:42 
Questioncapture image automatic Pin
Member 125746329-Jun-16 2:11
Member 125746329-Jun-16 2:11 
AnswerRe: capture image automatic Pin
Chris Quinn9-Jun-16 2:30
Chris Quinn9-Jun-16 2:30 
AnswerRe: capture image automatic Pin
ZurdoDev9-Jun-16 2:33
professionalZurdoDev9-Jun-16 2:33 
AnswerRe: capture image automatic Pin
Pete O'Hanlon9-Jun-16 2:34
mvePete O'Hanlon9-Jun-16 2:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.