Mercurial > pub > ImplabNet
comparison Implab.Fx/ControlHelpers.cs @ 3:1e9583086e99
Added Impl.Fx
author | cin |
---|---|
date | Fri, 13 Sep 2013 12:54:28 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:aa367305156b | 3:1e9583086e99 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.Linq; | |
4 using System.Text; | |
5 using System.Windows.Forms; | |
6 | |
7 namespace Implab.Fx | |
8 { | |
9 public static class ControlHelpers | |
10 { | |
11 /// <summary> | |
12 /// Переключает обработку обещания в поток указанного элемента управления. | |
13 /// </summary> | |
14 /// <typeparam name="T">Тип результата обещания</typeparam> | |
15 /// <param name="that">Исходное обещание</param> | |
16 /// <param name="ctl">Элемент управления</param> | |
17 /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns> | |
18 /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception> | |
19 /// <example> | |
20 /// client | |
21 /// .Get("description.txt") // returns a promise | |
22 /// .DirectToControl(m_ctl) // handle the promise in the thread of the control | |
23 /// .Then( | |
24 /// description => m_ctl.Text = description // now it's safe | |
25 /// ) | |
26 /// </example> | |
27 public static Promise<T> DirectToControl<T>(this Promise<T> that, Control ctl) | |
28 { | |
29 if (that == null) | |
30 throw new ArgumentNullException("that"); | |
31 if (ctl == null) | |
32 throw new ArgumentNullException("ctl"); | |
33 | |
34 var directed = new Promise<T>(); | |
35 | |
36 that.Then( | |
37 res => | |
38 { | |
39 if (ctl.InvokeRequired) | |
40 ctl.Invoke(new Action<T>(directed.Resolve),res); | |
41 else | |
42 directed.Resolve(res); | |
43 }, | |
44 err => | |
45 { | |
46 if (ctl.InvokeRequired) | |
47 ctl.Invoke(new Action<Exception>(directed.Reject), err); | |
48 else | |
49 directed.Reject(err); | |
50 } | |
51 ); | |
52 | |
53 return directed; | |
54 } | |
55 } | |
56 } |