Debian Codegraph @

external/rx/Rx/NET/Samples/HOL/CS/Excercise8/Step11/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 // Turn the user input into a tamed sequence of strings.
23 var textChanged = from evt in Observable.FromEventPattern(txt, "TextChanged")
24 select ((TextBox)evt.Sender).Text;
25
26 var input = textChanged
27 .Throttle(TimeSpan.FromSeconds(1))
28 .DistinctUntilChanged();
29
30 // Bridge with the web service's MatchInDict method.
31 var svc = new DictServiceSoapClient("DictServiceSoap");
32 var matchInDict = Observable.FromAsyncPattern<string, string, string, DictionaryWord[]>
33 (svc.BeginMatchInDict, svc.EndMatchInDict);
34
35 Func<string, IObservable<DictionaryWord[]>> matchInWordNetByPrefix =
36 term => matchInDict("wn", term, "prefix");
37
38 // The grand composition connecting the user input with the web service.
39 var res = (from term in input
40 select matchInWordNetByPrefix(term))
41 .Switch();
42
43 // Synchronize with the UI thread and populate the ListBox or signal an error.
44 using (res.ObserveOn(lst).Subscribe(
45 words =>
46 {
47 lst.Items.Clear();
48 lst.Items.AddRange((from word in words select word.Word).ToArray());
49 },
50 ex =>
51 {
52 MessageBox.Show(
53 "An error occurred: " + ex.Message, frm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error
54 );
55 }))
56 {
57 Application.Run(frm);
58 } // Proper disposal happens upon exiting the application.
59 }
60 }
61 }