/// <summary> /// Begins the wrapped operation. /// </summary> /// <typeparam name="T">The type of the operation result.</typeparam> /// <param name="begin">The begin.</param> /// <param name="end">The end.</param> /// <param name="callback">The callback.</param> /// <param name="state">The state.</param> /// <returns>The async result.</returns> public static IAsyncResult BeginWrap <T>(AsyncBegin begin, AsyncEnd <T> end, AsyncCallback callback, object state) { Requires.NotNull(begin, "begin"); Requires.NotNull(end, "end"); return(new AsyncResultWrapper <T>(begin, end, callback, state)); }
/// <summary> /// Initializes a new instance of the <see cref="AsyncResultWrapper"/> class. /// </summary> /// <param name="begin">The begin.</param> /// <param name="end">The end.</param> /// <param name="callback">The callback.</param> /// <param name="state">The state.</param> internal AsyncResultWrapper(AsyncBegin begin, AsyncEnd end, AsyncCallback callback, object state) { this.end = end; this.callback = callback; this.state = state; this.result = begin(this.OnCompleted, null); }
/// <summary>Simplified calling convention for asynchronous Begin/End operations.</summary> /// <typeparam name="T">The type of data returned by the async operation.</typeparam> /// <param name="begin">The start (Begin*) of the async operation.</param> /// <param name="end">The end (End*) of the async operation.</param> /// <param name="callback">The operation to perform once the operation has completed and a value received.</param> /// <param name="exceptionHandler">Callback to invoke when an excetption is thrown during the async operation.</param> public static void RunAsync <T>( AsyncBegin <T> begin, AsyncEnd <T> end, Action <T> callback, Action <Exception> exceptionHandler) { begin(delegate(IAsyncResult ar) { T result; try { result = end(ar); } catch (Exception ex) { if (exceptionHandler != null) { exceptionHandler(ex); } return; } callback(result); }, null); }