我正在尝试创建一个接受TcpClient连接并在客户端连接后执行任务的方法“ConnectedAction”.当尝试创建一个新的任务来运行委托“ConnectedAction”时,我收到编译错误.
Argument 1: cannot convert from ‘void’ to ‘System.Func’
我相信这个错误是因为该方法试图运行“ConnectedAction”方法并返回void到Task.Run参数.
如何让Task运行“ConnectedAction”委托?
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)); } } }