/// <summary> /// Returns a method that works as the begin method of an APM version of the backing method provided. /// </summary> /// <param name="backingMethod">Method to wrap in APM.</param> /// <returns>APM method</returns> public static Func <AsyncCallback, object, IAsyncResult> CreateNoResultBeginMethod(Action backingMethod) { return(delegate(AsyncCallback callback, object state) { AsyncResultNoResult ar = new AsyncResultNoResult(callback, state); ThreadPool.QueueUserWorkItem((x) => { AsyncResultNoResult result = (AsyncResultNoResult)x; try { backingMethod(); result.SetAsCompleted(null, false); } catch (Exception e) { result.SetAsCompleted(e, false); } }, ar); return ar; }); }
/// <summary> /// Returns a method that wraps the backing method in APM. The backing method is a method which takes ONE parameter, defined /// as the type argumnt, TParameter. /// </summary> /// <typeparam name="TParameter">Type of parameter taken by backingMethod</typeparam> /// <param name="backingMethod">Method to be wrapped in APM</param> /// <returns>APM wrapped method</returns> public static Func <TParameter, AsyncCallback, object, IAsyncResult> CreateNoResultBeginMethod <TParameter>(Action <TParameter> backingMethod) { return(delegate(TParameter parameter, AsyncCallback callback, object state) { AsyncResultNoResult <TParameter> ar = new AsyncResultNoResult <TParameter>(callback, state, parameter); ThreadPool.QueueUserWorkItem((x) => { AsyncResultNoResult <TParameter> result = (AsyncResultNoResult <TParameter>)x; try { backingMethod(parameter); result.SetAsCompleted(null, false); } catch (Exception e) { result.SetAsCompleted(e, false); } }, ar); return ar; }); }
/// <summary> /// Generic End method that can be used where there is no return value. /// </summary> /// <param name="asyncResult">ar from a Begin method</param> public static void NoResultEndMethod(IAsyncResult asyncResult) { AsyncResultNoResult ar = (AsyncResultNoResult)asyncResult; ar.EndInvoke(); }