exception:在已经完成 任务后,尝试将任务转换为最终状态。
来源:互联网 发布:linux rpm qa grep 编辑:程序博客网 时间:2024/05/23 16:04
refs:
https://stackoverflow.com/questions/15316613/when-should-taskcompletionsourcet-be-used
https://technet.microsoft.com/zh-cn/library/dd449174(v=vs.110).aspx
https://code.msdn.microsoft.com/Windows-Phone-8-Networking-835239c1/sourcecode?fileId=70769&pathId=221052408
相关
https://hmemcpy.com/2013/01/turning-old-and-busted-asynchronous-code-into-new-asyncawait-enabled-hotness-with-taskcompletionsourcet/
An attempt was made to transition a task to a final state when it had already completed
TaskCompletionSource is used to create Task objects that don't execute code.
In Real World Scenarios TaskCompletionSource is ideal for I/O bound operations.
This way you get all the benefits of tasks (e.g. return values, continuations etc) without blocking a thread for the duration of the operation.
If your "function" is an IO bound operation it isn't recommended to block a thread using a new Task.
Instead using TaskCompletionSource you can create a slave task to just indicate when your I/O bound operation finishes or faults.
TaskCompletionSource 对于非task型方法,并且有事务需要处理,可返回task,并带上result或exception
如(only a event based api is available )
TaskCompletionSource<object> completionSource; public Task SendUsingManagedSocketsAsync(string strServerIP) { // enable asynchronous task completion completionSource = new TaskCompletionSource<object>(); // create a new socket var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // create endpoint var ipAddress = IPAddress.Parse(strServerIP); var endpoint = new IPEndPoint(ipAddress, PORT); // create event args var args = new SocketAsyncEventArgs(); args.RemoteEndPoint = endpoint; args.Completed += SocketConnectCompleted; // check if the completed event will be raised. If not, invoke the handler manually. if (!socket.ConnectAsync(args)) SocketConnectCompleted(args.ConnectSocket, args); return completionSource.Task; } private void SocketConnectCompleted(object sender, SocketAsyncEventArgs e) { // check for errors if (e.SocketError != System.Net.Sockets.SocketError.Success) { completionSource.SetException(new Exception("Failed with " + e.SocketError)); // do some resource cleanup CleanUp(e); return; } else { completionSource.SetResult(null); } // check what has been executed switch (e.LastOperation) { case SocketAsyncOperation.Connect: HandleConnect(e); break; case SocketAsyncOperation.Send: HandleSend(e); break; } }
阅读全文
0 0
- exception:在已经完成 任务后,尝试将任务转换为最终状态。
- 在页面加载完成后执行任务
- 任务状态转换
- 登录服务器windows2008出现:远程桌面服务当前正忙,因此无法完成您尝试执行的任务。请在几分钟后重试。其他用户应该仍然能够登录
- 登录服务器windows2008出现:远程桌面服务当前正忙,因此无法完成您尝试执行的任务。请在几分钟后重试。其他用户应该仍然能够登录
- 系统任务状态转换框架
- VxWorks任务状态的转换
- android异步任务完成后再执行主线程任务
- Android中如何在子线程执行任务完成后更新UI
- 在已完成的任务之间选择
- linux进程任务状态转换图
- spark任务已经执行结束,但还显示RUNNING状态
- 第二周 任务2.2 将读入的字符转换为整数输出
- OC用Dispatch处理并发完成所有任务之后执行最终的结果
- 执行ant构建文件过后,没有修改任何相关的文件,第二遍执行ant,这时ant会提示任务已经完成的任务不再执行,记住这是任务级别的,这种情况包括在控制台上指定两个project
- 页面添加下载任务,后台开启线程异步下载,下载完成更新任务状态
- 任务完成后的退出码是(e0434f4d)。
- 服务器虚拟化后需要完成的八大关键任务
- PHP常用函数
- java生成指定个数及区间范围的不重复随机数存入数组中
- 多个程序共享同一串口数据的实现
- 利用Caffe创建自己的lmdb数据集
- select默认选中
- exception:在已经完成 任务后,尝试将任务转换为最终状态。
- 【CodeForces 55D】Beautiful number 数位DP
- Math.random()使用-抽奖概率事件
- PHP常用的文件操作函数
- 测试
- 在线视频播放
- iphone手机safari输入法不支持keyup事件解决方法
- 对SpringIOC和DI的个人理解
- php 获取今日、昨日、上周、本月的起始时间戳和结束时间