ximalangyashan 2022-12-12 21:01 采纳率: 98.9%
浏览 5
已结题

C#中,Task Action错误

C#中,Task Action错误

代码这一部分,Task task1 = new Task(action,null);
提示报错原因是: 参数 1: 无法从“System.Action”转换为“System.Action”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApp中task的状态
{
    class Program
    {
        private static void Method(int i)
        {
            i =i *1000;
            Thread.Sleep(i);
        }
        static void Main(string[] args)
        {
            Action<int> action = new Action<int>(Method);
            Task task1 = new Task(action,null);
            task1.Start();
        }
    }
}

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-12-12 21:26
    关注

    task的重载没有int这种泛型,只有Action,你传递Action当然没法转换成action,改成下面就行了

    
    
            private static void Method(object state)
            {
                var o = (Data)state;
                int a = o.x;
                int b = o.y;
                int i = a * b;
                Console.WriteLine($"{a}{b}相乘的结果是{i}");
            }
            static void Main(string[] args)
            {
                Action<object> action = new Action<object>(Method);
                Task task1 = new Task(action, new Data { x=1,y=2});
                task1.Start();
                Console.ReadKey();
            }
            public class Data { public int x { get; set; }public int y { get; set; } }
    
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月20日
  • 已采纳回答 12月12日
  • 创建了问题 12月12日

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭