Execute code when a timer expires

1

I have an application that receives data sent from another computer through UDP. My problem is that I need to know when the other team stops sending messages through the network, because when listening to the listener, it only does that ... and I have no way of knowing when to stop listening to decide that it is not There are more messages.

Searching for a solution, someone proposed me to use timers, for which start a timmer when the listener starts, and jump to another routine when the time expires (5 sec for example). The point is, that the timer never executes this action.

here is the code

class Program
{
    static void Main(string[] args)
    {
        int localPort = 15000;
        IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 0);

        UdpClient client = new UdpClient(localPort);
        UdpState state = new UdpState(client, remoteSender);

        // inicia el timmer
        System.Timers.Timer timerUp = new System.Timers.Timer(5000);
        timerUp.Elapsed += new System.Timers.ElapsedEventHandler(CheckStatus);

        // inicia la rececion de mensajes
        client.BeginReceive(new AsyncCallback(DataReceived), state);

        // espera hasta que se pulse una tecla para salir
        // al quedarse esperando aca, el timer nunca se dispara
        Console.ReadKey();
        client.Close();
    }

    static void CheckStatus(object sender, EventArgs e)
    {
        // aca nunca llega el programa, porque el timmer no se dispara
        EstadoCarro.Instancia.IdCarro = 1;
        EstadoCarro.Instancia.Estado = "offLine";
    }


    private static void DataReceived(IAsyncResult ar)
    {
        UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
        IPEndPoint wantedIpEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
        IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
        Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);

        bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address)) || wantedIpEndPoint.Address.Equals(IPAddress.Any);
        bool isRightPort = (wantedIpEndPoint.Port == receivedIpEndPoint.Port) || wantedIpEndPoint.Port == 0;
        if (isRightHost && isRightPort)
        {
            string receivedText = ASCIIEncoding.ASCII.GetString(receiveBytes);
            Console.Write(receivedText);

            if (receivedText.Length > 0)
            {
                EstadoCarro.Instancia.IdCarro = 1;
                EstadoCarro.Instancia.Estado = "Online";
            }
        }

        // reinicia el listener
        c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);

    }

}

here I'm running a class with the singleton pattern, so I can have a single instance in case I need to see more devices

public class EstadoCarro
{
    public static EstadoCarro Instancia = new EstadoCarro();

    private EstadoCarro()
    { }

    public int IdCarro { get; set; }
    public string Estado { get; set; }

}

Any idea what I'm doing wrong?

    
asked by Luis Gabriel Fabres 17.04.2017 в 17:56
source

1 answer

3

I do not see in your code that you call timerUp.Start() . If you do not indicate Timer that starts working, you will never execute CheckStatus

    
answered by 17.04.2017 / 18:04
source