Start Thread with Input Parameters for Void Method

Asked

Viewed 1,489 times

1

I try 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 continues running. But I need to pass two values for this method. The fragment of the method that will be pointed out in Thread can be seen below.

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

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 if my method was:

public void Processamento() { ... }

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

Processamento(addr, line);
  • 1

    Your question was unclear. After all, what is your question? You can [Dit] your question at any time to add relevant information.

  • I edited the question to make it clearer.

  • It got a little better, you can already understand what you intend to do. Now tell me: what is Data?

  • You didn’t get it the way I told you?

  • It gave a bug in another part of the code , but for that purpose it worked,, thank you

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));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.