高分求大神帮忙

qq_38234381 2019-08-03 03:47:39
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text;

namespace Exercise11
{
class Program
{
static int Test1(int x, string s)
{
Console.WriteLine("Test1");
Thread.Sleep(100);
return 100;
}
static void Main(string[] args)
{
Func<int, string, int> a = Test1;
IAsyncResult ar = a.BeginInvoke(100, "tqt", null, null);//编译器提示这里有错
Console.WriteLine("main");
while (ar.IsCompleted == false)
Console.Write(".");
int result = a.EndInvoke(ar);
Console.WriteLine(result);
}
}
}

在VS2019中编译通过,运行时错误信息是System.PlatformNotSupportedException: Operation is not supported on this platform.
是BeginInvoke这个函数的错误吗?求大神。
...全文
198 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_38234381 2019-08-04
  • 打赏
  • 举报
回复
感谢各位的热心解答,换种写法就解决了。
XBodhi. 2019-08-04
  • 打赏
  • 举报
回复


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Task<int> task= Test1(100, "tqt");

if (task.IsCompleted == false)
{
Console.Write(".");
}
else
{
Console.WriteLine(task.Result);
}

}


static async Task<int> Test1(int x, string s)
{
Console.WriteLine("Test1");
Thread.Sleep(100);
return 100;
}
}
}


stherix 2019-08-03
  • 打赏
  • 举报
回复
.net core 貌似不支持Func BeginInvoke 尝试用通用一点的方法代替吧 比如Task.Run await之类的改写
stherix 2019-08-03
  • 打赏
  • 举报
回复
.NET Core也能运行的啊
github_36000833 2019-08-03
  • 打赏
  • 举报
回复
新建项目时,如果'.Net Core'类别外,找不到'Windows Desktop'类别, 那么试试再运行一次Visual Studio Installer安装程序,选择’修改‘ 并添加勾选‘Dotnet Desktop Development'选项。
qq_38234381 2019-08-03
  • 打赏
  • 举报
回复
请问选择项目模板时是要选择ASP.NET Core Web 应用程序吗还是同样选择控制台应用,选择控制台应用的话我只能找到.Net Core这个目标框架,大佬能详细说下吗?
github_36000833 2019-08-03
  • 打赏
  • 举报
回复
新建一个项目,更快。
qq_38234381 2019-08-03
  • 打赏
  • 举报
回复
请问大佬我创建项目时是控制台应用(.NET Core),如何才能换成“DotNet Framework 4.0或更高”
github_36000833 2019-08-03
  • 打赏
  • 举报
回复
可能你的项目设置成了Dotnet Core了。 解决方法有两种, 一种是把项目换成DotNet Framework 4.0或更高。如果你是学习为目的,就用这种。 一种就是不用Delegate.BeginInvoke,DotNet Core摒弃了这种调用方式。你可以改用Task.Run来异步调用。

static void Main(string[] args)
{
    var task = Task.Run<int>(() => Test1(100, "tqt"));  // DotNet Core下改成Task.Run
    while(task.IsCompleted == false)
        Console.Write(".");
    int result = task.Result;
    Console.WriteLine(result);
}

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧