none
Task.Run 不可以直接传入方法名? RRS feed

  • 问题

  • 最新的.net的支持给委托变量传方法名,Run带有个Action的参数,

    Task.Run(DoWork),编译器提示 DoWork CS0407,函数 DoWork 声明一致为什么还有问题?


    • 已编辑 itenjoy 2015年11月10日 1:07
    2015年11月10日 1:07

答案

  • 你好,

    我在测试的时候 Task.Run(DoWork) 遇到了这个错误  The call is ambiguous between the following methods or properties: 'System.Threading.Tasks.Task.Run(System.Action)' and 'System.Threading.Tasks.Task.Run(System.Func<System.Threading.Tasks.Task>)'  看上去是类型转换的问题。 直接传方法名字,framework会和Func这个重载去匹配。

    解决办法非常简单。

     Task.Run(new Action(DoWork));

    Task

    .Run(DoWork);


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年11月11日 5:59
  • 我用的是visual studio 2015 Community, 下载地址: https://www.visualstudio.com/en-us/products/vs-2015-product-editions.aspx

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年11月12日 1:08

全部回复

  • 你好,

    我在测试的时候 Task.Run(DoWork) 遇到了这个错误  The call is ambiguous between the following methods or properties: 'System.Threading.Tasks.Task.Run(System.Action)' and 'System.Threading.Tasks.Task.Run(System.Func<System.Threading.Tasks.Task>)'  看上去是类型转换的问题。 直接传方法名字,framework会和Func这个重载去匹配。

    解决办法非常简单。

     Task.Run(new Action(DoWork));

    Task

    .Run(DoWork);


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年11月11日 5:59
  • 非常感谢,我用了

     Task.Run(ActionDoWork);

    请问你用的vs版本多少,我怎么没提示这个?

    2015年11月11日 8:22
  • 我用的是visual studio 2015 Community, 下载地址: https://www.visualstudio.com/en-us/products/vs-2015-product-editions.aspx

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年11月12日 1:08