Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a very simple custom Task.Run() method.
Using the .net Task.Run() method works fine, the custom method does not execute the action.


Any ideas?

What I have tried:

Usage:
//works
      Task.Run(() => TestTasks.CustomTasks.DummyTask());

      //does not work
      TestHelpers.Tasks.Run(TestTasks.CustomTasks.DummyTask);

Code:
namespace TestHelpers
{
    public static class Tasks
    {
        public static string Run(Action action)
        {
            try
            {
                Task.Run(() => action);
                return "ok";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }
    }
}

namespace TestTasks
{
    public static class CustomTasks
    {
        public static void DummyTask()
        {
            for (int i = 0; i < 10; i++)
                System.Threading.Thread.Sleep(100);
        }
    }
}
Posted
Updated 18-Feb-19 22:55pm

1 solution

You must replace
Task.Run(() => action);

by
Task.Run(action);

Please have a look at Task.Run Method (System.Threading.Tasks) | Microsoft Docs[^]
The syntax is
public static System.Threading.Tasks.Task Run (Action action);
 
Share this answer
 
v2
Comments
Sni.DelWoods 19-Feb-19 5:57am    
Great, this works. Thank you!

By the way:
Why do I have use the method directly instead of using a lambda?
As I can see the .net method has the same signature: Run (Action action)

And why do I HAVE to use a lambda if my method uses parameters?
 public static void DummyTask(string param) {...}

 TestHelpers.Tasks.Run(() => TestTasks.CustomTasks.DummyTask("My parameter"));
TheRealSteveJudge 19-Feb-19 6:10am    
You're welcome.
According to the documentation the method 'Run' has an object of type 'Action' as parameter.
Your 'TestHelpers.Tasks.Run' Method also has an object of type 'Action' as parameter.
Sni.DelWoods 19-Feb-19 7:10am    
Yes, that's what I detected. But why can't I use a lambda ()=>method.
I would have expected there's no difference between my Task.Run and the Threading.Tasks.Task.Run.
(Just want to understand what's happening here...Some things won't get in my mind...)
Richard Deeming 20-Feb-19 9:25am    
Task.Run(action)
- Returns a Task which executes the specified action.
Task.Run(() => action());
- Returns a Task which executes an anonymous method which executes the action.
Task.Run(() => action);
- Returns a Task<Action> which executes an anonymous method which returns the action. The action is not executed.
TheRealSteveJudge 19-Feb-19 7:56am    
TestHelpers.Tasks.Run(new Action(TestTasks.CustomTasks.DummyTask));

is the same as
TestHelpers.Tasks.Run(TestTasks.CustomTasks.DummyTask);

In
Task.Run(() => TestTasks.CustomTasks.DummyTask());
you have brackets.

There is no need to write 'new Action(TestTasks.CustomTasks.DummyTask)' as it is a redundant explicit delegate creation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900