Program.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. .Where(term => term.Length >= 3)
  22. //.Throttle(TimeSpan.FromSeconds(1))
  23. .DistinctUntilChanged()
  24. .Do(Console.WriteLine);
  25. var svc = new DictServiceSoapClient("DictServiceSoap");
  26. var matchInDict = Observable.FromAsyncPattern<string, string, string, DictionaryWord[]>
  27. (svc.BeginMatchInDict, svc.EndMatchInDict);
  28. Func<string, IObservable<DictionaryWord[]>> matchInWordNetByPrefix =
  29. term => matchInDict("wn", term, "prefix");
  30. var res = from term in input
  31. from words in matchInWordNetByPrefix(term)
  32. select words;
  33. using (res.ObserveOn(lst).Subscribe(
  34. words =>
  35. {
  36. lst.Items.Clear();
  37. lst.Items.AddRange((from word in words select word.Word).ToArray());
  38. },
  39. ex =>
  40. {
  41. MessageBox.Show(
  42. "An error occurred: " + ex.Message, frm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error
  43. );
  44. }))
  45. {
  46. Application.Run(frm);
  47. }
  48. }
  49. }
  50. }