| @@ -1,30 +1,30 | |||||
| 1 | using System.Windows.Forms; |
|
1 | using System.Windows.Forms; | |
| 2 | using System; |
|
2 | using System; | |
| 3 |
|
3 | |||
| 4 |
|
4 | |||
| 5 | namespace Implab.Fx { |
|
5 | namespace Implab.Fx { | |
| 6 | public class ControlBoundPromise<T> : Promise<T> { |
|
6 | public class ControlBoundPromise<T> : Promise<T> { | |
| 7 | readonly Control m_target; |
|
7 | readonly Control m_target; | |
| 8 |
|
8 | |||
| 9 | public ControlBoundPromise(Control target) { |
|
9 | public ControlBoundPromise(Control target) { | |
| 10 | Safe.ArgumentNotNull(target, "target"); |
|
10 | Safe.ArgumentNotNull(target, "target"); | |
| 11 |
|
11 | |||
| 12 | m_target = target; |
|
12 | m_target = target; | |
| 13 | } |
|
13 | } | |
| 14 |
|
14 | |||
| 15 |
public ControlBoundPromise(Control target, IPromise parent |
|
15 | public ControlBoundPromise(Control target, IPromise parent) | |
| 16 |
: base(parent |
|
16 | : base(parent) { | |
| 17 | Safe.ArgumentNotNull(target, "target"); |
|
17 | Safe.ArgumentNotNull(target, "target"); | |
| 18 |
|
18 | |||
| 19 | m_target = target; |
|
19 | m_target = target; | |
| 20 | } |
|
20 | } | |
| 21 |
|
21 | |||
| 22 | protected override void InvokeHandler(HandlerDescriptor handler) { |
|
22 | protected override void InvokeHandler(HandlerDescriptor handler) { | |
| 23 | if (m_target.InvokeRequired) |
|
23 | if (m_target.InvokeRequired) | |
| 24 | m_target.BeginInvoke(new Action<HandlerDescriptor>(base.InvokeHandler), handler); |
|
24 | m_target.BeginInvoke(new Action<HandlerDescriptor>(base.InvokeHandler), handler); | |
| 25 | else |
|
25 | else | |
| 26 | base.InvokeHandler(handler); |
|
26 | base.InvokeHandler(handler); | |
| 27 | } |
|
27 | } | |
| 28 | } |
|
28 | } | |
| 29 | } |
|
29 | } | |
| 30 |
|
30 | |||
| @@ -1,41 +1,41 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 | using System.Windows.Forms; |
|
2 | using System.Windows.Forms; | |
| 3 | using System.Threading; |
|
3 | using System.Threading; | |
| 4 |
|
4 | |||
| 5 | namespace Implab.Fx |
|
5 | namespace Implab.Fx | |
| 6 | { |
|
6 | { | |
| 7 | public static class PromiseHelpers |
|
7 | public static class PromiseHelpers | |
| 8 | { |
|
8 | { | |
| 9 | /// <summary> |
|
9 | /// <summary> | |
| 10 | /// Перенаправляет обработку обещания в поток указанного элемента управления. |
|
10 | /// Перенаправляет обработку обещания в поток указанного элемента управления. | |
| 11 | /// </summary> |
|
11 | /// </summary> | |
| 12 | /// <typeparam name="T">Тип результата обещания</typeparam> |
|
12 | /// <typeparam name="T">Тип результата обещания</typeparam> | |
| 13 | /// <param name="that">Исходное обещание</param> |
|
13 | /// <param name="that">Исходное обещание</param> | |
| 14 | /// <param name="ctl">Элемент управления</param> |
|
14 | /// <param name="ctl">Элемент управления</param> | |
| 15 | /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns> |
|
15 | /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns> | |
| 16 | /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception> |
|
16 | /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception> | |
| 17 | /// <example> |
|
17 | /// <example> | |
| 18 | /// client |
|
18 | /// client | |
| 19 | /// .Get("description.txt") // returns a promise |
|
19 | /// .Get("description.txt") // returns a promise | |
| 20 | /// .DispatchToControl(m_ctl) // handle the promise in the thread of the control |
|
20 | /// .DispatchToControl(m_ctl) // handle the promise in the thread of the control | |
| 21 | /// .Then( |
|
21 | /// .Then( | |
| 22 | /// description => m_ctl.Text = description // now it's safe |
|
22 | /// description => m_ctl.Text = description // now it's safe | |
| 23 | /// ) |
|
23 | /// ) | |
| 24 | /// </example> |
|
24 | /// </example> | |
| 25 | public static IPromise<T> DispatchToControl<T>(this IPromise<T> that, Control ctl) |
|
25 | public static IPromise<T> DispatchToControl<T>(this IPromise<T> that, Control ctl) | |
| 26 | { |
|
26 | { | |
| 27 | Safe.ArgumentNotNull(that, "that"); |
|
27 | Safe.ArgumentNotNull(that, "that"); | |
| 28 | Safe.ArgumentNotNull(ctl, "ctl"); |
|
28 | Safe.ArgumentNotNull(ctl, "ctl"); | |
| 29 |
|
29 | |||
| 30 |
var directed = new ControlBoundPromise<T>(ctl,that |
|
30 | var directed = new ControlBoundPromise<T>(ctl,that); | |
| 31 |
|
31 | |||
| 32 | that.Last( |
|
32 | that.Last( | |
| 33 | directed.Resolve, |
|
33 | directed.Resolve, | |
| 34 | directed.Reject, |
|
34 | directed.Reject, | |
| 35 | directed.Cancel |
|
35 | directed.Cancel | |
| 36 | ); |
|
36 | ); | |
| 37 |
|
37 | |||
| 38 | return directed; |
|
38 | return directed; | |
| 39 | } |
|
39 | } | |
| 40 | } |
|
40 | } | |
| 41 | } |
|
41 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
