Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从不同的类和参数启动线程_C#_.net_Multithreading - Fatal编程技术网

C# 从不同的类和参数启动线程

C# 从不同的类和参数启动线程,c#,.net,multithreading,C#,.net,Multithreading,我想用在外部类中实现的方法启动线程,并且我需要将一些外部属性的引用传递给此方法 问题就在这里 Thread t = new Thread(Agent.Activate(agentParameters, ref tcpListener)); VisualStudio说Acvivate方法应该返回Thread。但通过下面的例子,它不应该这样做 我使用这个例子,但它没有帮助。这是我的密码 class TCPListenerManager { TcpListener t

我想用在外部类中实现的方法启动
线程
,并且我需要将一些外部属性的引用传递给此方法

问题就在这里

Thread t = new Thread(Agent.Activate(agentParameters, ref tcpListener));
VisualStudio说
Acvivate
方法应该返回
Thread
。但通过下面的例子,它不应该这样做

我使用这个例子,但它没有帮助。这是我的密码

    class TCPListenerManager
    {
        TcpListener tcpListener;

        public TCPListenerManager(HostListenerItem hostListenerItem)
        {
            tcpListener = new TcpListener(IPAddress.Parse(hostListenerItem.IP4), hostListenerItem.Port);

            for (int i = 0; i < hostListenerItem.ClientsMax; i++)
            {
                var agentParameters = new AgentParameters();
                Thread t = new Thread(Agent.Activate(agentParameters, ref tcpListener));
                t.Start();
            }
        }

    } // end of class DeviceAgent


 class Agent
    {
        [ThreadStaticAttribute]
        static int threadSpecificData;

        static public AgentParameters Parameters;

        public static void Activate(AgentParameters agentParameters, ref TcpListener tcpListener)
        {
            Parameters = agentParameters;
            threadSpecificData = Thread.CurrentThread.ManagedThreadId;

            var socket = tcpListener.AcceptSocket();
            if (socket.Connected)
            {
                //
                // logger.Info("Socket.Connected on" + socketParameters.HostListenerItem.Name + " " + socketParameters.HostListenerItem.Url);
            }
        }
    } // end of Agent


    class AgentParameters
    {
        public HostListenerItem HostListenerItem { get; set; }

        public AgentParameters()
        {
            HostListenerItem = new HostListenerItem();
        }
    }
类TCPListenerManager
{
TcpListener TcpListener;
公共TCPListenerManager(HostListenerItem HostListenerItem)
{
tcpListener=newtcplistener(IPAddress.Parse(hostListenerItem.IP4),hostListenerItem.Port);
对于(int i=0;i
您必须将方法作为委托传递给新线程:

Thread t = new Thread(() => Agent.Activate(agentParameters, ref tcpListener));

您必须将方法作为委托传递给新线程:

Thread t = new Thread(() => Agent.Activate(agentParameters, ref tcpListener));

这个ClientsMax不是应该的样子。启动1个在循环中接受的线程,并为每个接受的客户端启动一个线程<代码>如果(socket.Connected)也不起作用。这个ClientsMax不是它应该的样子。启动1个在循环中接受的线程,并为每个接受的客户端启动一个线程<代码>如果(socket.Connected)也不执行任何操作。