Exemple #1
0
 /// <summary>
 /// <para>Observe on UIDispatcherScheduler.</para>
 /// <para>UIDIspatcherScheduler is created when access to UIDispatcher.Default first in the whole application.</para>
 /// <para>If you want to explicitly initialize, call UIDispatcherScheduler.Initialize() in App.xaml.cs.</para>
 /// If a new value arrives before the selector is finished calculated the old one then it is canceled.
 ///
 /// This will also raise a message box if an exception is thrown downstream.
 /// </summary>
 public static IObservableExceptional <U> ObserveOnSolidworksThread <T, U>(this IObservableExceptional <T> source, Func <T, CancellationToken, U> selector)
 {
     return(source
            .StartWith(default(T))
            .Select(s => new { s, cts = new CancellationTokenSource() })
            .Buffer(2, 1).Where(b => b.Count == 2)
            .Select
                (b =>
     {
         b[0].cts.Cancel();
         return b[1];
     })
            .ObserveOn(UiDispatcherScheduler.Default)
            .Select(b => selector(b.s, b.cts.Token)));
 }
Exemple #2
0
 public static IObservableExceptional <Task> ObserveOnSolidworksThread <T>(this IObservableExceptional <T> source, Func <T, CancellationToken, Task> selector)
 {
     return(source
            .StartWith(default(T))
            .Select(s => new { s, cts = new CancellationTokenSource() })
            .Buffer(2, 1).Where(b => b.Count == 2)
            .Select(b =>
     {
         b[0].cts.Cancel();
         return b[1];
     })
            .ObserveOn(UiDispatcherScheduler.Default)
            .Select(async b =>
     {
         try
         {
             await selector(b.s, b.cts.Token);
         }
         catch (OperationCanceledException)
         {
             Console.WriteLine("Operation cancelled");
         }
     }));
 }