How can I pass parameters to the method I want to start in a new thread?

1

Suppose I have something like that;

    Thread hilo = new Thread(NuevoHilo);
    hilo.Start();

But the method NuevoHilo has a parameter of type string.

    private void NuevoHilo(string item)
            {

            }

How can I load parameters?

Thanks

    
asked by Edulon 01.10.2017 в 17:11
source

1 answer

2

Good day, a serious solution with a Lambda expression, in this way you can send n parameters. I leave you a small example.

    public class Program
    {
        public void Main()
        {
            string name= "Jhon";
            string lastname = "Wick"
            Thread th = new Thread(() => NewThread(name, lastname));
            th.Start();
        }

        public void NewThread(string name, string lastname)
        {
            Console.WriteLine(name + " " + lastname);
        }
    }

I hope and it is the solution. Greetings.

    
answered by 02.10.2017 / 21:51
source