title: Live update map - Timer class - c#
body: <p>I'm building a project with asp.net. 
  Part of the the project is a view (using google maps api) that is showing the the status of the parking lots with maerkers on the map.
  Im using JSON file to create the markers.</p>
  
  <p>Moreover, Im using arduino with some sensors that are indicated of the parking lot status.</p>
  
  <p>I want that this Json will be update (override the previous) <strong>every 2 seconds</strong> (so that if a car enters the parking lot and now its full - it will present on the map as full)</p>
  
  <p>I have 2 functions that creates this Json's and I want to call them every 2 seconds as I said before.</p>
  
  <p>I could not do it. I'll be glad to receive your help.</p>
  
  <p>The name of the view page: "TotalPs".</p>
  
  <p>This is the controller in which the relevant function is located:</p>
  
  <pre><code>    public ActionResult TotalPs()
      {
  
          ViewBag.Message = "TotalPs";
          return View();
  
      }
  
      public ActionResult TotalPData()
      {
          ReadArduino(); //READ THE DATA FROM THE ARDUINO
          callA(); // CREATES THE FIRST JSON
          callB(); // CREATES THE 2ND JSON
  
  
          var totalQueryParkingLot =
                  from lot in db.parkingLots
                  orderby lot.PricePerHour
                  select lot;
  
          return Json(totalQueryParkingLot);
      }
  
      public void callA()
      {
  
          var totalQueryParkingLot =
                  from lot in db.parkingLots
                  orderby lot.PricePerHour
                  select lot;
  
          var data2 = totalQueryParkingLot.ToList();
  
          var jsonString2 = JsonConvert.SerializeObject(data2);
  
          if (jsonString2 != null)
          {
              if (!Directory.Exists(Server.MapPath("~/Content/")))
              {
                  Directory.CreateDirectory(Server.MapPath("~/Content/"));
              }
  
          }
  
          System.IO.File.WriteAllText(Server.MapPath("~/Content/TotalJsonPL.json"), jsonString2);
  
      }
  
      public void callB()
      {
  
          var FreeQueryParkingLot =
             from pub in db.publicParkings
             orderby pub.PricePerHourpublicParking
             select pub;
  
          var data8 = FreeQueryParkingLot.ToList();
  
          var jsonString3 = JsonConvert.SerializeObject(data8);
  
          if (jsonString3 != null)
          {
              if (!Directory.Exists(Server.MapPath("~/Content/")))
              {
                  Directory.CreateDirectory(Server.MapPath("~/Content/"));
              }
  
          }
  
          System.IO.File.WriteAllText(Server.MapPath("~/Content/TotalJsonPU.json"), jsonString3);
  
      }
  
          public void ReadArduino()
      {
          SerialPort port = new SerialPort("COM3", 9600);
          port.BaudRate = 9600;
          port.PortName = "COM3";
          port.Open();
  
          bool status1 = true;
          bool status2 = true;
          bool status3 = true;
          char[] arr = new char[4];
  
  
         String data_arduino = port.ReadLine();
  
  
              for (int i = 0; i &lt; arr.Length; i++)
              {
                  char first = data_arduino[i];
                  arr[i] = first;
              }
  
  
          int space = arr[0] - 48;
  
          var arduinoQuery1 = from b in db.parkingLots where b.parkingLotID == 22 select b;
  
          foreach (parkingLot parkingLot in arduinoQuery1)
          {
              parkingLot.freeSpaces = space;
          }
          db.SaveChanges();
  
      }
  </code></pre>
  
  <p>In the view I call the function TotalPData() that is calling to the other functions.</p>
  
  <p><a href="https://i.stack.imgur.com/9X6E5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9X6E5.png" alt="View"></a></p>
  
  <p><strong>Tnx!!</strong></p>