title: How to make a functional never ending task/loop? - C# with serialport
body: <p>So I'm creating a Windows Forms app that's gonna continously display updated sensor data from my Arduino microcontroller through serialport. I am also rather new to C# and even more so, Arduino. My issue is that when I try to display the updated sensor data through timers - the app crashes. </p>
  
  <p>The arduino sends a string formatted like this <em>1;25</em> - where the first number <em>1</em> or <em>0</em>, says whether or not the motion sensor has detected movement. The other number is the temperature.</p>
  
  <pre><code>class ArduinoComClass : SerialPort
  {
      public string receivedData;
      private bool dataReady = false;
      public ArduinoComClass(string comport, int baudrate)
      {
          PortName = comport;
          BaudRate = baudrate;
          DataReceived += new SerialDataReceivedEventHandler(dataReceived);
          ReadTimeout = 2000;
          Open();
      }
  
      ~ArduinoComClass()
      {
          Close();
      }
  
      public void ConvertString(out string pirValue, out string tempValue)
      {
          string[] data = receivedData.Split(';');
          string string1 = data[0];
          if (string1 == "1")
          {
              pirValue = "Movement detected!";
          }
          else
          {
              pirValue = "No movement detected";
          }
          string string2 = data[1];
          tempValue = string2;
      }
  
      public bool DataReady
      {
          get
          {
              return dataReady;
          }
          set
          {
              dataReady = value;
          }
      }
      public void dataReceived(object sender, SerialDataReceivedEventArgs earg)
      {
          try
          {
              receivedData = ReadLine();
              dataReady = true;
          }
          // catch(TimeoutException e)
          catch (Exception e)
          {
              MessageBox.Show(e.Message);
              dataReady = false;
          }
      }
  }
  </code></pre>
  
  <p>Now this is the code for the form I want to display the data in. It is very messed up due to me trying and trying different things and just kept on making it worse. I've tried tasks/threading but I get an error due to the fact that receivedData is in another thread.</p>
  
  <pre><code>    static private ArduinoComClass ArduinoCom = new ArduinoComClass("COM3", 9600);
      private string pirValue;
      private string tempValue;
  
      public Main()
      {
          InitializeComponent();
          pirValue = "";
          tempValue = "";
          tmrAlarm.Start();
      }
  
      private void ShowSensorInfo()
      {
          while (true)
          {
              if (ArduinoCom.DataReady)
              {
                  ArduinoCom.ConvertString(out pirValue, out tempValue);
                  txtShowTemp.Text = tempValue;
                  if (pirValue == "Movement detected!")
                  {
                      picPIR.BackColor = Color.Red;
                  }
                  else
                  {
                      picPIR.BackColor = Color.Lime;
                  }
  
                  //if (SystemInformation.PowerStatus.PowerLineStatus) &lt;- Ignore for now
                  ArduinoCom.DataReady = false;
              }
          }
      }
  
      private async void tmrAlarm_Tick(object sender, EventArgs e)
      {
          try
          {
              Task myTask = Task.Run(() =&gt; ShowSensorInfo());
              await myTask;
              myTask.Wait(2000);
          }
          catch (Exception error)
          {
              Console.WriteLine(error.Message);
          }
      }
  </code></pre>
  
  <p>So in short - how can I in an easy and effective way make my program show this live data without having the app crash? </p>