title: TimerElapsedEvent not working in UDP Agent in C#
body: <p>My server is running continuously to receive data from Agent, but the Agent is getting closed after a single run.
  Even my <code>timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);</code> is not working properly.</p>
  
  <p>Agent Code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Net;
  using System.Net.Sockets;
  using System.Timers;
  using System.Threading;
  
  namespace HeartBeatAgent
  {
  public class Agent
  {
  
      static string clientdata = "IP1 Version 1";
      static UdpClient UdpAgent = new UdpClient();
      static System.Timers.Timer timer = new System.Timers.Timer();
      static int ip = 127001;
      static int port = 6060;
      static IPEndPoint receivePoint;
  
  
  
      static void Main(string[] args)
      {
          UdpAgent = new UdpClient(port);
          receivePoint = new IPEndPoint(new IPAddress(ip), port);
          Thread startClient = new Thread(new ThreadStart(startAgent));
          //Start the Thread
          startClient.Start();
      }
  
      public static void startAgent()
      {
  
          Console.WriteLine("This is Agent");
  
          //Send Data
          System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
          string sendString = clientdata.ToString();
          byte[] sendMsg = encode.GetBytes(sendString);
  
          //Send to Server to corresponding port of server
          UdpAgent.Send(sendMsg, sendMsg.Length, "localhost", 6767);
          Console.WriteLine("Msg sent to server 4m agent");
  
  
          //Receive Data from Server
          byte[] recMsg = UdpAgent.Receive(ref receivePoint);
          System.Text.ASCIIEncoding receive = new System.Text.ASCIIEncoding();
          //Split it up
          string[] temp = receive.GetString(recMsg).Split(new Char[] { ' ' });
          Console.WriteLine(temp[2] + temp[1] + temp[0]);
  
          **timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);**
          timer.Interval = 10000;
          timer.Start();
  
      }
  
      private static void OnTimedEvent(object source, ElapsedEventArgs e)
      {
          startAgent();
      }
  
  }
  }
  </code></pre>
  
  <p>Server Code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Net;
  using System.Net.Sockets;
  
  namespace HeartBeatServer
  {
  public class Server
  {
      public static void Main(string[] args)
      {
          UdpClient UdpServer = new UdpClient(6767);
  
          int count = 0;
          while (true)
          {
              try
              {
  
                  Dictionary&lt;string, int&gt; agentCount = new Dictionary&lt;string, int&gt;();
  
                  Console.WriteLine("This is server");
                  //Define a Receive point
                  IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
  
                  //Receive Data from Agent
                  byte[] recData = UdpServer.Receive(ref RemoteIpEndPoint);
                  System.Text.ASCIIEncoding encode = new System.Text.ASCIIEncoding();
                  //Split it up by space
                  string[] temp = encode.GetString(recData).Split(new Char[] { ' ' });
                  if (agentCount.ContainsKey(temp[0]))
                  {
                      var coun = agentCount[temp[0]];
  
                      agentCount.Add(temp[0], coun++);
                      Console.WriteLine(coun);
  
                  }
                  else
                  {
                      agentCount.Add(temp[0], count);
                      Console.WriteLine(temp[0], count);
                      //Re-send the Data to Agent
                      byte[] sendData = encode.GetBytes(System.DateTime.Now.ToString());
                      UdpServer.Send(sendData, sendData.Length, "localhost", 6060);
                      Console.WriteLine("Reply Message Sent to Agent 4m Server");
                  }
  
  
                  Console.WriteLine(temp[0] + temp[1] + temp[2]);
                  continue;
              }
  
  
              catch (Exception ex)
              {
                  throw ex;
              }
          }
  
      }
  }
  }
  </code></pre>
  
  <p>I need to send message continuously to server at time interval of 5-10 seconds. Able to achieve first time only. Any idea where I am going wrong?</p>