Program.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Reactive;
  3. using System.Reactive.Linq;
  4. using System.Windows.Forms;
  5. namespace Excercise5
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. var txt = new TextBox();
  12. var frm = new Form()
  13. {
  14. Controls = { txt }
  15. };
  16. var input = (from evt in Observable.FromEventPattern(txt, "TextChanged")
  17. select ((TextBox)evt.Sender).Text)
  18. .LogTimestampedValues(x => Console.WriteLine("I: " + x.Timestamp.Millisecond + " - " + x.Value))
  19. .Throttle(TimeSpan.FromSeconds(1))
  20. .LogTimestampedValues(x => Console.WriteLine("T: " + x.Timestamp.Millisecond + " - " + x.Value))
  21. .DistinctUntilChanged();
  22. using (input.Subscribe(inp => Console.WriteLine("User wrote: " + inp)))
  23. {
  24. Application.Run(frm);
  25. }
  26. }
  27. }
  28. public static class MyExtensions
  29. {
  30. public static IObservable<T> LogTimestampedValues<T>(this IObservable<T> source, Action<Timestamped<T>> onNext)
  31. {
  32. return source.Timestamp().Do(onNext).Select(x => x.Value);
  33. }
  34. }
  35. }