/// <summary> /// Wraps a <see cref="Task{TResult}" /> into the Begin method of an APM pattern. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="task">The task to wrap. May not be <c>null</c>.</param> /// <param name="callback">The callback method passed into the Begin method of the APM pattern.</param> /// <param name="state">The state passed into the Begin method of the APM pattern.</param> /// <returns> /// The asynchronous operation, to be returned by the Begin method of the APM pattern. /// </returns> public static IAsyncResult ToBegin <TResult>(Task <TResult> task, AsyncCallback callback, object state) { TaskCompletionSource <TResult> tcs = new TaskCompletionSource <TResult>(state, TaskCreationOptions.RunContinuationsAsynchronously); SynchronizationContextSwitcher.NoContext(() => CompleteAsync(task, callback, tcs)); return(tcs.Task); }
/// <summary> /// Wraps a <see cref="Task"/> into the Begin method of an APM pattern. /// </summary> /// <param name="task">The task to wrap.</param> /// <param name="callback">The callback method passed into the Begin method of the APM pattern.</param> /// <param name="state">The state passed into the Begin method of the APM pattern.</param> /// <returns>The asynchronous operation, to be returned by the Begin method of the APM pattern.</returns> public static IAsyncResult ToBegin(Task task, AsyncCallback callback, object state) { var tcs = new TaskCompletionSource <object>(state, TaskCreationOptions.RunContinuationsAsynchronously); using (SynchronizationContextSwitcher.NoContext()) CompleteAsync(task, callback, tcs); return(tcs.Task); }
public override Task <int> ReadAsync([NotNull] byte[] buffer, int offset, int count, CancellationToken token) { CheckDisposed(); return(SynchronizationContextSwitcher.NoContext(async() => { count = Math.Min(count, _row.ColumnLen - _row.PosInColumn); var read = await _row.Buffer.ReadAllBytes(buffer, offset, count, true, true); _row.PosInColumn += read; return read; })); }