N
N
NeoLight32020-08-12 15:42:22
C++ / C#
NeoLight3, 2020-08-12 15:42:22

How to create a function with parameters in a stream?

I looked all over the Internet, I could not find a normal answer on how to create a new thread with a function with parameters in it.
Here is my code:

<br>
        static void Main(string[] args)<br>
        {<br>
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);<br>
            socket.Bind(new IPEndPoint(IPAddress.Any, 25444));<br>
            socket.Listen(32);<br>
            while (true)<br>
            {<br>
                    var clientSocket = socket.Accept();<br>
                    var endpoint = new IPEndPoint("127.0.0.1", 25443);<br>
                    var thread = new Thread(ParameterizedThreadStart(UdpClientReceive(clientSocket, endpoint)));<br>
                    thread.Start();<br>
            }<br>
}<br>

At the same time, it gives an error: "The non-callable member of ParameterizedThreadStart cannot be used as a method ".

Here is the function code with parameters:
<br>
        private static void UdpClientReceive(Socket socket, IPEndPoint endpoint)<br>
        {<br>
            var udpClient = new UdpClient();<br>
            udpClient.Connect(endpoint);<br>
            var clientBuffer = new byte[4096];<br>
            while (true)<br>
            {<br>
                socket.Receive(clientBuffer);<br>
                udpClient.Send(clientBuffer, clientBuffer.Length);<br>
            }<br>
        }<br>


How then in general it is possible to deduce function with parameters in a separate flow?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2020-08-12
@NeoLight3

Is it possible to call it through another function?
like this

Thread thread = new Thread(() =>   {
               unt_client_recv(client, "127.0.0.1", 25443);
            });

If not, then you need to design it as a class, and initially create a class, initialize it and pass the method to be executed on the thread

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question