Start Thread with Input Parameters for Void Method

1

I tried to initialize a Thread that has the function of processing a certain information by a method, so that the main execution line of the program keeps running. But I need to pass two values to this method. The fragment of the method that will be pointed to in Thread can be seen below.

public class Dados
{
   public void Processamento(ulong address, string line)
   {
       string[] data = line.Split('||');
       //...
       radio.SendAndWaitForResponse(address,information);
       ...
   }
}

The Thread would be instantiated as follows:

int index = 0;
ulong addr = 0x00;
string line = "";
//...

public List<Dados> Data new List<Dados>();
public  List<Thread> Processos = new List<Thread>();
//...

Processos.Add(new Thread(Data[index].Processamento))
Processos[index].Start();
index++;

That's where my method was:

public void Processamento() { ... }

But I wish I could pass the value of the addr and line variables when instantiating the Thread, as if it were to use the method normally. p>

Processamento(addr, line);
    
asked by anonymous 02.08.2016 / 17:03

1 answer

2

If I understand correctly, what you need is simply this

var thread = new Thread(() => Data[index].Processamento(addr, line));
Processos.Add(thread));
    
02.08.2016 / 17:57