vs2019创建.net core3.1 的控制台应用程序

执行以下代码:

using System;
using System.Diagnostics;
using System.Threading; namespace ConsoleApp3
{
/// <summary>
/// 委托必须和要调用的异步方法有相同的签名
/// </summary>
/// <param name="callDuration">sleep时间</param>
/// <param name="threadId">当前线程id</param>
/// <returns></returns>
public delegate string AsyncMethodCaller(int callDuration, out int threadId);
class Program
{ /// <summary>
/// 主函数
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
AsyncMethodCaller caller = new AsyncMethodCaller(TestMethodAsync);
int threadid = ;
//开启异步操作
IAsyncResult result = caller.BeginInvoke(, out threadid, null, null);
for (int i = ; i < ; i++)
{
Console.WriteLine("其它业务" + i.ToString());
}
//调用EndInvoke,等待异步执行完成
Console.WriteLine("等待异步方法TestMethodAsync执行完成");
string res = caller.EndInvoke(out threadid, result);
Console.WriteLine("Completed!");
Console.WriteLine(res);
Console.Read();
}
/// <summary>
/// 与委托对应的方法
/// </summary>
/// <param name="callDuration"></param>
/// <param name="threadId"></param>
/// <returns></returns>
static string TestMethodAsync(int callDuration, out int threadId)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine("异步TestMethodAsync开始");
for (int i = ; i < ; i++)
{ // 模拟耗时操作
Thread.Sleep(callDuration);
Console.WriteLine("TestMethodAsync:" + i.ToString());
}
sw.Stop();
threadId = Thread.CurrentThread.ManagedThreadId;
return string.Format("耗时{0}ms.", sw.ElapsedMilliseconds.ToString());
}
}
}

IAsyncResult result = caller.BeginInvoke(3000, out threadid, null, null);

所在行提示错误

System.PlatformNotSupportedException
HResult=0x80131539
Message=Operation is not supported on this platform.
Source=ConsoleApp3
StackTrace:
at ConsoleApp3.AsyncMethodCaller.BeginInvoke(Int32 callDuration, Int32& threadId, AsyncCallback callback, Object object)
at ConsoleApp3.Program.Main(String[] args) in C:\Users\pxm\source\repos\ConsoleApp3\ConsoleApp3\Program.cs:line

原因

The Asynchronous Programming Model (APM) (using IAsyncResult and BeginInvoke) is no longer the preferred method of making asynchronous calls. The Task-based Asynchronous Pattern (TAP) is the recommended async model as of .NET Framework 4.5. Because of this, and because the implementation of async delegates depends on remoting features not present in .NET Core, BeginInvoke and EndInvoke delegate calls are not supported in .NET Core. This is discussed in GitHub issue dotnet/corefx #5940.

异步编程模型(APM)(使用IAsyncResult和BeginInvoke)不再是异步调用的优选方法。从.NET Framework 4.5开始,基于任务的异步模式(TAP)是推荐的异步模型。因此,而且由于异步委托的实现取决于远程处理但.NET Core不存在的功能,BeginInvoke和EndInvoke委托调用不.NET Core支持。GitHub问题 dotnet/corefx#5940 中对此进行了讨论

Async delegates are not in .NET Core for several reasons:

  • Async delegates use deprecated IAsyncResult-based async pattern. This pattern is generally not supported throughout .NET Core base libraries, e.g. System.IO.Stream does not have IAsyncResult-based overloads for Read/Write methods in .NET Core.
  • Async delegates depend on remoting (System.Runtime.Remoting) under the hood. Remoting is not in .NET Core - implementation of async delegates without remoting would be challenging.

解决办法

改为基于任务的异步模式Task.Run

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks; namespace ConsoleApp3
{
/// <summary>
/// 委托必须和要调用的异步方法有相同的签名
/// </summary>
/// <param name="callDuration">sleep时间</param>
/// <param name="threadId">当前线程id</param>
/// <returns></returns>
public delegate string AsyncMethodCaller(int callDuration, out int threadId);
class Program
{ /// <summary>
/// 主函数
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
AsyncMethodCaller caller = new AsyncMethodCaller(TestMethodAsync);
int threadid = ;
//开启异步操作
//IAsyncResult result = caller.BeginInvoke(3000, out threadid, null, null);
var workTask = Task.Run(() => caller.Invoke(, out threadid)); for (int i = ; i < ; i++)
{
Console.WriteLine("其它业务" + i.ToString());
}
//调用EndInvoke,等待异步执行完成
Console.WriteLine("等待异步方法TestMethodAsync执行完成");
//string res = caller.EndInvoke(out threadid, result);
string res = workTask.Result; Console.WriteLine("Completed!");
Console.WriteLine(res);
Console.Read();
}
/// <summary>
/// 与委托对应的方法
/// </summary>
/// <param name="callDuration"></param>
/// <param name="threadId"></param>
/// <returns></returns>
static string TestMethodAsync(int callDuration, out int threadId)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine("异步TestMethodAsync开始");
for (int i = ; i < ; i++)
{ // 模拟耗时操作
Thread.Sleep(callDuration);
Console.WriteLine("TestMethodAsync:" + i.ToString());
}
sw.Stop();
threadId = Thread.CurrentThread.ManagedThreadId;
return string.Format("耗时{0}ms.", sw.ElapsedMilliseconds.ToString());
}
}
}

参考:

1.Migrating Delegate.BeginInvoke Calls for .NET Core

2.初试WPF代码迁移Core WPF

System.PlatformNotSupportedException:“Operation is not supported on this platform.”的更多相关文章

  1. 解决 cmake_symlink_library: System Error: Operation not supported

    在编译uchardet时遇到这个错误: cmake_symlink_library: System Error: Operation not supported 创建链接不成功,要确认当前帐户下是否有 ...

  2. firefox同步ajax请求报错的问题 A parameter or an operation is not supported by the underlying object

    今天在测试系统时,一个很正常的功能在firefox下报错,经过验证在ie和chrome浏览器中功能这个正常.   调试后发现: 请求比其他请求的特殊点在于同步请求.   经过firefox的控制台上测 ...

  3. Eclipse进度条出现“Remote System Explorer Operation”解决方法

    Eclipse进度条出现“Remote System Explorer Operation”解决方法  

  4. 【转载】Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法

    原来是eclipse后台进程在远程操作,就是右下角显示的“Remote System Explorer Operation”.折腾了半天,在Stack Overflow找到答案(源地址).把解决方案翻 ...

  5. ORA-27300: OS system dependent operation:sendmsg failed with status: 105 ORA-27301: OS failure message: No buffer space available

    早上查看数据库alert日志,发现如下ORA-报错: kgxpvfynet: mtype: 61 process 6460 failed because of a resource problem i ...

  6. Remote System Explorer Operation总是运行后台服务,卡死eclipse

    阿里云 > 教程中心 > android教程 > Remote System Explorer Operation总是运行后台服务,卡死eclipse Remote System E ...

  7. Remote System Explorer Operation在eclipse后台一直跑 解决办法

    在用eclipse开发时,经常遇到卡死的情况,其中一种就是右下角出现:“Remote System Explorer Operation”,解决方案如下: 第一步:Eclipse -> Pref ...

  8. Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法

    当你右键编辑控件的id或者其他属性时都会卡很久,发现原来是eclipse后台进程在远程操作,就是右下角显示的“Remote System Explorer Operation”.折腾了半天,在Stac ...

  9. eclipse remote system explorer operation

    Remote System Explorer Operation卡死Eclipse解决方案 - 披萨大叔的博客 - CSDN博客https://blog.csdn.net/qq_27258799/ar ...

随机推荐

  1. js性能优化之---防抖函数

    使用场景 input的时时触发搜索功能 scroll事件的滚动条位置的监测 resize事件监听窗口变化等 举个栗子(input框时时触发搜索功能) 普通未防抖款 var textElement = ...

  2. 8.4 Go select

    8.4 Go select Go语言引入了select关键字,用于处理异步IO问题,语义和switch特别相似.语法由select开始,每个条件由case语句来描述.每个case语句必须是IO操作. ...

  3. apache.zookeeper-3.4与apache.kafka-2.11的安装

                                           zookeeper与Kafka集群安装 集群安装以三台机器(虚拟机,物理机等等)为例子: 192.168.200.100 ...

  4. Linux下搭建mongDB环境

    参考: https://blog.csdn.net/qq_35763837/article/details/79654023 https://www.linuxidc.com/Linux/2016-0 ...

  5. MyBatis通过注解方式批量添加、修改、删除

    唯能极于情,故能极于剑 注: 本文转载于:CodeCow · 程序牛 的个人博客:http://www.codecow.cn/ 一.数据库实体DO public class User implemen ...

  6. 11.Java连接Redis_Jedis_测试联通

    使用Java开发项目的时候使用Redis的话,目前有一些开源API可以使用. 最常用的就是jedis,它提供了许多基于Java的对象和方法来调用Redis的指令. jedis的jar包下载地址http ...

  7. [工具推荐]001.FlipPDF使用教程

    FlipPDF是一个什么样的软件呢,他有什么实际用途呢?顾名思义,这是一个跟PDF有关的软件,没错它是一款把PDF转换成酷炫书籍的软件,他还支持PDF中的目录,也就是转换成的书籍,目录一样可以跳转的. ...

  8. 高吞吐量的分布式发布订阅消息系统Kafka之Producer源码分析

    引言 Kafka是一款很棒的消息系统,今天我们就来深入了解一下它的实现细节,首先关注Producer这一方. 要使用kafka首先要实例化一个KafkaProducer,需要有brokerIP.序列化 ...

  9. POJ 2671 Jimmy's Bad Day题解(很详细很友好,类似区间dp)

    有问题的话欢迎在评论区提出 题意: 题目链接 你是一个送快递的,现在给你一个环,环的边有权值,代表走这条边所花的时间,每个点代表一个地点,点有点权,代表这个点上有多少货物需要你送.初始时间\(t=0\ ...

  10. Java实现 蓝桥杯 算法训练 大小写转换

    算法训练 大小写转换 时间限制:1.0s 内存限制:512.0MB 提交此题 问题描述 编写一个程序,输入一个字符串(长度不超过20),然后把这个字符串内的每一个字符进行大小写变换,即将大写字母变成小 ...