C# AsyncCallback.?.Invoke方法代码示例

2021-11-18  乐帮网

c#

本文整理汇总了C#中AsyncCallback.?.Invoke方法的典型用法代码示例。如果您正苦于以下问题:C# AsyncCallback.?.Invoke方法的具体用法?C# AsyncCallback.?.Invoke怎么用?C# AsyncCallback.?.Invoke使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AsyncCallback的用法示例。
在下文中一共展示了AsyncCallback.?.Invoke方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

public void BeginConnectProxy(EndPoint remoteEP, AsyncCallback callback, object state)
        {
            // do nothing

            var r = new FakeAsyncResult(state);
            callback?.Invoke(r);
        }
/// <summary>
        /// 
        /// </summary>
        /// <param name="task"></param>
        /// <param name="callback"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public static IAsyncResult ToBegin(Task task, AsyncCallback callback, object state)
        {
            if (task == null)
                throw new ArgumentNullException(nameof(task));

            var tcs = new TaskCompletionSource<object>(state);
            task.ContinueWith(t =>
            {
                if (task.IsFaulted)
                {
                    if (task.Exception != null)
                        tcs.TrySetException(task.Exception.InnerExceptions);
                }
                else if (task.IsCanceled)
                {
                    tcs.TrySetCanceled();
                }
                else
                {
                    tcs.TrySetResult(null);
                    //tcs.TrySetResult(RpcAction.GetTaskResult(t));
                }

                callback?.Invoke(tcs.Task);
            }/*, TaskScheduler.Default*/);

            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(state);
            task.ContinueWith(t => {
                tcs.TryCompleteFromCompletedTask(t);

                callback?.Invoke(tcs.Task);
            }, TaskScheduler.Default);

            return tcs.Task;
        }
public LazyAsyncResult(SslState sslState, object asyncState, AsyncCallback asyncCallback)
 {
     AsyncState = asyncState;
     asyncCallback?.Invoke(this);
 }
/// <summary>
        /// Begins and asynchronous read of the specified stream
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="buffer">The buffer.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="count">The count.</param>
        /// <param name="callback">The callback.</param>
        /// <param name="state">The state.</param>
        /// <returns></returns>
        public static IAsyncResult BeginRead(this Stream stream, byte[] buffer,
            int offset,
            int count,
            AsyncCallback callback,
            object state)
        {
            var result = new AsyncResult(state);

            Task.Run(() =>
            {
                try
                {
                    var data = stream.Read(buffer, offset, count);
                    result.Complete(data);
                    callback?.Invoke(result);
                }
                catch (IOException)
                {
                    // Ignore, possible connection closed
                }
            });

            return result;
        }
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     Write(buffer, offset, count);
     var tcs = new TaskCompletionSource<object>(state);
     tcs.TrySetResult(null);
     IAsyncResult result = tcs.Task;
     callback?.Invoke(result);
     return result;
 }

原文章地址:https://vimsky.com/examples/detail/csharp-ex---AsyncCallback-%3F.Invoke-method.html

公众号二维码

关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com

庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。

欧阳修

付款二维码

如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力