Debian Codegraph @

external/rx/Rx/NET/Samples/HOL/CS/Excercise8/Step10/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 from word in matchInWordNetByPrefix(term)
41 // .Finally(() => Console.WriteLine("Disposed request for " + term))
42 .TakeUntil(input)
43 select word;
44
45 // Synchronize with the UI thread and populate the ListBox or signal an error.
46 using (res.ObserveOn(lst).Subscribe(
47 words =>
48 {
49 lst.Items.Clear();
50 lst.Items.AddRange((from word in words select word.Word).ToArray());
51 },
52 ex =>
53 {
54 MessageBox.Show(
55 "An error occurred: " + ex.Message, frm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error
56 );
57 }))
58 {
59 Application.Run(frm);
60 } // Proper disposal happens upon exiting the application.
61 }
62 }
63 }