How to create loop thread in c#?

Asked

Viewed 749 times

0

How do I create a thread that runs a function, wait 1 second and loop again until the program shuts down?

1 answer

2


I don’t know what your role will do but this would be a Loop Thread.

using System.Threading;

...

Thread thread = new Thread(tarefa);
thread.Start();

public void tarefa()
{
  while(true)
  {
     ...
     anotherFunction();
     Thread.Sleep(1000);
  } 
}
public void anotherfunction()
{
  ...
}

Summarizing Voce creates a Thread pointing to the task() method that loops (in this case eternal) the anotherfunction() method and waits for a second to run again

  • 1

    That’s what I was wanting to do, I just forgot to use the Sleep function.

  • For beauty, do not forget to use a Thread.Abort(); in your program, otherwise this Thread will be "open" forever, in a simple use may not make a difference, but on a large scale, as for example use it for communication, Forgetting to abort overloads processing.

Browser other questions tagged

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