Program.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Linq;
  3. using System.Reactive.Linq;
  4. using System.Windows.Forms;
  5. using System.Reactive.Disposables;
  6. using Excercise8.DictionarySuggestService;
  7. namespace Excercise8
  8. {
  9. class Program
  10. {
  11. static void Main()
  12. {
  13. var txt = new TextBox();
  14. var lst = new ListBox { Top = txt.Height + 10 };
  15. var frm = new Form()
  16. {
  17. Controls = { txt, lst }
  18. };
  19. var input = (from evt in Observable.FromEventPattern(txt, "TextChanged")
  20. select ((TextBox)evt.Sender).Text)
  21. .Throttle(TimeSpan.FromSeconds(1))
  22. .DistinctUntilChanged()
  23. .Do(x => Console.WriteLine(x));
  24. var svc = new DictServiceSoapClient("DictServiceSoap");
  25. var matchInDict = Observable.FromAsyncPattern<string, string, string, DictionaryWord[]>
  26. (svc.BeginMatchInDict, svc.EndMatchInDict);
  27. Func<string, IObservable<DictionaryWord[]>> matchInWordNetByPrefix =
  28. term => matchInDict("wn", term, "prefix");
  29. var res = from term in input
  30. from words in matchInWordNetByPrefix(term)
  31. select words;
  32. using (res.ObserveOn(lst).Subscribe(words =>
  33. {
  34. lst.Items.Clear();
  35. lst.Items.AddRange((from word in words select word.Word).ToArray());
  36. }))
  37. {
  38. Application.Run(frm);
  39. }
  40. }
  41. }
  42. }