Trigger method 5 seconds after running the previous one

0

How to put a timer so that after sending the command EnvDados , the command NovoEnvio is sent, 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);
    
asked by anonymous 13.05.2016 / 16:19

1 answer

1

You do not need a timer for this.

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 that the method is running, but in some cases this is not a problem.

s.Send(EnvDados);

Thread.Sleep(5000);

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