Debian Codegraph @

external/rx/Rx/NET/Samples/HOL/CS/Excercise8/Step09/Program.cs

raw
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
8 namespace Excercise8
9 {
10 class Program
11 {
12 static void Main()
13 {
14 var txt = new TextBox();
15 var lst = new ListBox { Top = txt.Height + 10 };
16
17 var frm = new Form()
18 {
19 Controls = { txt, lst }
20 };
21
22 var input = (from evt in Observable.FromEventPattern(txt, "TextChanged")
23 select ((TextBox)evt.Sender).Text)
24 .Where(term => term.Length >= 3)
25 //.Throttle(TimeSpan.FromSeconds(1))
26 .DistinctUntilChanged()
27 .Do(Console.WriteLine);
28
29 var svc = new DictServiceSoapClient("DictServiceSoap");
30 var matchInDict = Observable.FromAsyncPattern<string, string, string, DictionaryWord[]>
31 (svc.BeginMatchInDict, svc.EndMatchInDict);
32
33 Func<string, IObservable<DictionaryWord[]>> matchInWordNetByPrefix =
34 term => matchInDict("wn", term, "prefix");
35
36 var res = from term in input
37 from words in matchInWordNetByPrefix(term)
38 select words;
39
40 using (res.ObserveOn(lst).Subscribe(
41 words =>
42 {
43 lst.Items.Clear();
44 lst.Items.AddRange((from word in words select word.Word).ToArray());
45 },
46 ex =>
47 {
48 MessageBox.Show(
49 "An error occurred: " + ex.Message, frm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error
50 );
51 }))
52 {
53 Application.Run(frm);
54 }
55 }
56 }
57 }