Mercurial > pub > ImplabNet
view Implab.Fx/ControlHelpers.cs @ 3:1e9583086e99
Added Impl.Fx
author | cin |
---|---|
date | Fri, 13 Sep 2013 12:54:28 +0400 |
parents | |
children |
line wrap: on
line source
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace Implab.Fx { public static class ControlHelpers { /// <summary> /// Переключает обработку обещания в поток указанного элемента управления. /// </summary> /// <typeparam name="T">Тип результата обещания</typeparam> /// <param name="that">Исходное обещание</param> /// <param name="ctl">Элемент управления</param> /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns> /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception> /// <example> /// client /// .Get("description.txt") // returns a promise /// .DirectToControl(m_ctl) // handle the promise in the thread of the control /// .Then( /// description => m_ctl.Text = description // now it's safe /// ) /// </example> public static Promise<T> DirectToControl<T>(this Promise<T> that, Control ctl) { if (that == null) throw new ArgumentNullException("that"); if (ctl == null) throw new ArgumentNullException("ctl"); var directed = new Promise<T>(); that.Then( res => { if (ctl.InvokeRequired) ctl.Invoke(new Action<T>(directed.Resolve),res); else directed.Resolve(res); }, err => { if (ctl.InvokeRequired) ctl.Invoke(new Action<Exception>(directed.Reject), err); else directed.Reject(err); } ); return directed; } } }