Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Task.Run(Action<T>)

I am attempting to create a method that accepts TcpClient connections and performs a task once a client is connected, "ConnectedAction". I am receiving a compile error when trying to have a new task created to run the delegate "ConnectedAction".

Argument 1: cannot convert from 'void' to 'System.Func'

I believe that this error is because the method is trying to run the "ConnectedAction" method and return void to the Task.Run parameter.

How do I have the Task run the "ConnectedAction" delegate?

class Listener
{
    public IPEndPoint ListenerEndPoint {get; private set;}
    public int TotalAttemptedConnections { get; private set; }
    public Action<TcpClient> ConnectedAction { get; private set; }

    public Listener(IPEndPoint listenerEndPoint, Action<TcpClient> connectedAction)
    {
        ConnectedAction = connectedAction;
        ListenerEndPoint = listenerEndPoint;

        Task.Factory.StartNew(Listen, TaskCreationOptions.LongRunning);
    }

    private void Listen()
    {
        TcpListener tcpListener = new TcpListener(ListenerEndPoint);
        tcpListener.Start();

        while (true)
        {
            TcpClient tcpClient = tcpListener.AcceptTcpClient();
            TotalAttemptedConnections++;

            //Error here 
            Task.Run(ConnectedAction(tcpClient));
        }
    }
}
like image 696
James Harmon Avatar asked Feb 20 '13 01:02

James Harmon


People also ask

How do I run a Task in C#?

To start a task in C#, follow any of the below given ways. Use a delegate to start a task. Task t = new Task(delegate { PrintMessage(); }); t. Start();

How do I create an action Task?

To create an action task: Add the action task to the ruleflow diagram in one of the following ways: Select the node before or after where you want to add an action task, and then click Add Node After > Action Task or Add Node Before > Action Task.

Should you use Task run?

If you use Task. Run with an I/O operation, you're creating a thread (and probably occupying a CPU core) that will mostly be waiting. It may be a quick and easy way to keep your application responsive, but it's not the most efficient use of system resources. A much better approach is to use await without Task.

How do I run a parallel Task in C#?

If you have several tasks that can be run in parallel, but still need to wait for all the tasks to end, you can easily achieve this using the Task. WhenAll() method in . NET Core. This will upload the first file, then the next file.


1 Answers

You should write:

Task.Run(() => ConnectedAction(tcpClient));

This creates a lambda function that takes no parameters and will call your specified function with the correct argument. The lambda is implicitly wrapped into the delegate type needed by the Task.Run parameters.

What you wrote calls the function and then attempts to turn the return value of the function into a delegate.

like image 74
David Pfeffer Avatar answered Oct 10 '22 02:10

David Pfeffer