Debian Codegraph @

external/rx/Rx/NET/Samples/HOL/CS/Excercise9/Step01/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 = new[] { "reac", "reactive", "bing" }.ToObservable();
33 #endif
34
35 // Bridge with the web service's MatchInDict method.
36 var svc = new DictServiceSoapClient("DictServiceSoap");
37 var matchInDict = Observable.FromAsyncPattern<string, string, string, DictionaryWord[]>
38 (svc.BeginMatchInDict, svc.EndMatchInDict);
39
40 Func<string, IObservable<DictionaryWord[]>> matchInWordNetByPrefix =
41 term => matchInDict("wn", term, "prefix");
42
43 // The grand composition connecting the user input with the web service.
44 var res = (from term in input
45 select matchInWordNetByPrefix(term))
46 .Switch();
47
48 // Synchronize with the UI thread and populate the ListBox or signal an error.
49 using (res.ObserveOn(lst).Subscribe(
50 words =>
51 {
52 lst.Items.Clear();
53 lst.Items.AddRange((from word in words select word.Word).ToArray());
54 },
55 ex =>
56 {
57 MessageBox.Show(
58 "An error occurred: " + ex.Message, frm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error
59 );
60 }))
61 {
62 Application.Run(frm);
63 } // Proper disposal happens upon exiting the application.
64 }
65 }
66 }