Debian Codegraph @

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