Fire method 5 seconds after execution of previous

Asked

Viewed 559 times

0

How to set a timer so that after 5 of sending the command EnvDados the sending of the command NovoEnvio, this within the same method?

IPAddress[] IPs = Dns.GetHostAddresses(host);
Socket s = new Socket(
               AddressFamily.InterNetwork, 
               SocketType.Stream, 
               ProtocolType.Tcp);

s.Connect(IPs[0], port);

// Recebe o Retorno após a conexão
byte[] buffer = new byte[60];
s.Receive(buffer);

// Envia um novo Comando
byte[] EnvDados = System.Text.Encoding.ASCII.GetBytes(ordem + "\n");
s.Send(EnvDados);

// Após 5 segundos enviar novo comando
byte[] NovoEnvio = System.Text.Encoding.ASCII.GetBytes(ordem + "\n");
s.Send(NovoEnvio);
  • Visual Studio 2012 framework 4.0

  • See if you can get some help: http://answall.com/q/30601/101, http://answall.com/q/87293/101, http://answall.com/a/86015/101

1 answer

1


You don’t need a timer for that.

If you are using C# 6

s.Send(EnvDados);

await Task.Sleep(5000);

byte[] NovoEnvio = System.Text.Encoding.ASCII.GetBytes(ordem + "\n");
s.Send(NovoEnvio);

In previous versions

Obviously, this will catch the thread the method is running, but in some cases this is no problem.

s.Send(EnvDados);

Thread.Sleep(5000);

byte[] NovoEnvio = System.Text.Encoding.ASCII.GetBytes(ordem + "\n");
s.Send(NovoEnvio);

Browser other questions tagged

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