3

I'm using the Task class in C# and want to pass a predefined method that returns a value and not using lambdas to the Task.Run method.

Here is a console app with the code:

static int ThreadMethod()
{
    return 42;
}

static void Main(string[] args)
{
    Task<int> t = Task.Run(function:ThreadMethod);
    WriteLine(t.Result);
}

However, it is returning this error:

The call is ambiguous between the following methods or properties: 'Task.Run<TResult>(Func<TResult>)' and 'Task.Run(Func<Task>)'

I tried doing this to fix it and got my expected result:

Task<int> t = Task.Run((Func<int>)ThreadMethod);

However, I am not sure if I'm doing it right or are there any better solution?

svick
  • 236,525
  • 50
  • 385
  • 514
  • Func and Action are just delegates. they have nothing just a name ;). use lambda instead. they are anonymous delegates btw. `()=>ThreadMethod()` – M.kazem Akhgary Nov 23 '15 at 06:38
  • by some tricks, if you dont want to use lambda for whatever reason. you can do this.`Task.Run(function: ThreadMethod)`. but you should change your method signature to this. `static async Task ThreadMethod()`. this is meaningless but it compiles. – M.kazem Akhgary Nov 23 '15 at 06:44
  • whoah the `async` part made it more confusing. I'm sorry I'm still a noob in multitasking and asynchronous programming in C# –  Nov 23 '15 at 06:52
  • Anyway, thanks for answering my query. In the mean time I'll just do what you and Cubicle Jockey suggested while looking for more information. –  Nov 23 '15 at 06:54
  • This Problem has been fixed in C# 7.3 see [this answer](https://stackoverflow.com/questions/51837722/why-is-the-call-ambiguous-task-runaction-and-task-runfunctask) – Tobias Hoefer Sep 11 '19 at 12:58

1 Answers1

3

Fix your .Run argument like in this example. Can be copied and pasted into LinqPad to test.

public static void Main(string[] args)
{
    Task<int> t = Task.Run(() => ThreadMethod());
    WriteLine(t.Result);
}

public static int ThreadMethod()
{
    return 42;
}

If you want to see it as a variable to pass check out below:

public static void Main(string[] args)
{

    //Func<int> is saying I want a function with no parameters
    //but returns an int. '() =>' this represents a function with 
    //no parameters. It then points to your defined method ThreadMethod
    //Which fits the notions of no parameters and returning an int.

    Func<int> threadMethod = () => ThreadMethod();

    Task<int> t = Task.Run(threadMethod);
    Console.WriteLine(t.Result);
}

public static int ThreadMethod()
{
    return 42;
}

Here is the Documentation on Func(T), on the left hand menu you can select the different variations of Func() objects.

Cubicle.Jockey
  • 3,288
  • 1
  • 19
  • 31
  • Can you further elaborate for me why this is a better way than casting to a (Func)? I just want to better understand it. Or maybe a good reference would really help. –  Nov 23 '15 at 06:47
  • Updated my answer a bit with a link to the documentation to study. – Cubicle.Jockey Nov 23 '15 at 06:53
  • Thank you. I'll look into all the information you gave and try to grasp it even more. –  Nov 23 '15 at 07:02