Program.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(
  33. words =>
  34. {
  35. lst.Items.Clear();
  36. lst.Items.AddRange((from word in words select word.Word).ToArray());
  37. },
  38. ex =>
  39. {
  40. MessageBox.Show(
  41. "An error occurred: " + ex.Message, frm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error
  42. );
  43. }))
  44. {
  45. Application.Run(frm);
  46. }
  47. }
  48. }
  49. }