annotate Implab.Fx/ControlHelpers.cs @ 3:1e9583086e99

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