Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 任务。工厂。开始新建<;TResult>;(Func<;对象、TResult>;、对象状态)生成错误_C#_Multithreading_Mono_Task Parallel Library - Fatal编程技术网

C# 任务。工厂。开始新建<;TResult>;(Func<;对象、TResult>;、对象状态)生成错误

C# 任务。工厂。开始新建<;TResult>;(Func<;对象、TResult>;、对象状态)生成错误,c#,multithreading,mono,task-parallel-library,C#,Multithreading,Mono,Task Parallel Library,当我编译代码时,我发现了这个错误,我不明白为什么mcs会查找错误的函数重载,我使用mono作为git当前活动开发版本的最新版本,我检查了TaskFactory类源代码,该函数存在 TaskPoc.cs(30,20):错误CS1502:System.Threading.Tasks.TaskFactory.StartNew(System.Func,System.Threading.Tasks.TaskCreationOptions)的最佳重载方法匹配具有一些无效参数 /usr/local/lib/

当我编译代码时,我发现了这个错误,我不明白为什么mcs会查找错误的函数重载,我使用mono作为git当前活动开发版本的最新版本,我检查了TaskFactory类源代码,该函数存在


TaskPoc.cs(30,20):错误CS1502:System.Threading.Tasks.TaskFactory.StartNew(System.Func,System.Threading.Tasks.TaskCreationOptions)的最佳重载方法匹配具有一些无效参数 /usr/local/lib/mono/4.5/mscorlib.dll(与先前错误相关的符号位置) TaskPoc.cs(30,56):错误CS1503:参数`#1'无法将`
System.Func
'表达式转换为类型`
System.Func
'


使用系统;
使用系统线程;
使用System.Threading.Tasks;
名称空间TaskPoc
{
公共阶级国家
{
公共整数;
公共字符串str;
}
公共类应用程序
{
公共静态void Main(字符串[]args)
{
状态=新状态();
state.num=5;
state.str=“helllllo”;
TaskCompletionSource TaskCompletionSource=新TaskCompletionSource(状态);
Task taskObj=taskCompletionSource.Task;
Func userMethod=(stateObj)=>
{
bool result=TestMethod(stateObj.num、stateObj.str);
taskCompletionSource.SetResult(结果);
返回结果;
};
Task.Factory.StartNew(userMethod,state);
bool result2=taskObj.Result;
WriteLine(“结果:,result2.ToString());
}
公共静态bool TestMethod(int num,string str)
{
睡眠(1000);
WriteLine(string.Format(“{0}{1}”,num,str));
返回true;
}
}
}

我假设您想要这个重载:

如您所见,
Func
的参数必须是
对象
,而不是
状态

您可以按如下方式修复代码:

Func<object, bool> userMethod = (state) =>
{
    State stateObj = (State)state;
    bool result = TestMethod(stateObj.num, stateObj.str);
    taskCompletionSource.SetResult(result);
    return result;
};
public static void Main(string[] args)
{
    int num = 5;
    string str = "Helllllllllllo";

    Task<bool> taskObj = Task.Run<bool>(() => TestMethod(num, str));

    bool result2 = taskObj.Result;
    Console.WriteLine("Result: {0}", result2);
}
Func userMethod=(state)=>
{
stateObj=(State)State;
bool result=TestMethod(stateObj.num、stateObj.str);
taskCompletionSource.SetResult(结果);
返回结果;
};

请注意,您的代码可以按如下方式缩短:

Func<object, bool> userMethod = (state) =>
{
    State stateObj = (State)state;
    bool result = TestMethod(stateObj.num, stateObj.str);
    taskCompletionSource.SetResult(result);
    return result;
};
public static void Main(string[] args)
{
    int num = 5;
    string str = "Helllllllllllo";

    Task<bool> taskObj = Task.Run<bool>(() => TestMethod(num, str));

    bool result2 = taskObj.Result;
    Console.WriteLine("Result: {0}", result2);
}
publicstaticvoidmain(字符串[]args)
{
int num=5;
string str=“helllllo”;
Task taskObj=Task.Run(()=>TestMethod(num,str));
bool result2=taskObj.Result;
WriteLine(“结果:{0}”,result2);
}

我假设您想要这个重载:

如您所见,
Func
的参数必须是
对象
,而不是
状态

您可以按如下方式修复代码:

Func<object, bool> userMethod = (state) =>
{
    State stateObj = (State)state;
    bool result = TestMethod(stateObj.num, stateObj.str);
    taskCompletionSource.SetResult(result);
    return result;
};
public static void Main(string[] args)
{
    int num = 5;
    string str = "Helllllllllllo";

    Task<bool> taskObj = Task.Run<bool>(() => TestMethod(num, str));

    bool result2 = taskObj.Result;
    Console.WriteLine("Result: {0}", result2);
}
Func userMethod=(state)=>
{
stateObj=(State)State;
bool result=TestMethod(stateObj.num、stateObj.str);
taskCompletionSource.SetResult(结果);
返回结果;
};

请注意,您的代码可以按如下方式缩短:

Func<object, bool> userMethod = (state) =>
{
    State stateObj = (State)state;
    bool result = TestMethod(stateObj.num, stateObj.str);
    taskCompletionSource.SetResult(result);
    return result;
};
public static void Main(string[] args)
{
    int num = 5;
    string str = "Helllllllllllo";

    Task<bool> taskObj = Task.Run<bool>(() => TestMethod(num, str));

    bool result2 = taskObj.Result;
    Console.WriteLine("Result: {0}", result2);
}
publicstaticvoidmain(字符串[]args)
{
int num=5;
string str=“helllllo”;
Task taskObj=Task.Run(()=>TestMethod(num,str));
bool result2=taskObj.Result;
WriteLine(“结果:{0}”,result2);
}