title: How to run multiple async tasks in one worker thread?
body: <ul>
  <li>Environment
  
  <ul>
  <li>Windows 7</li>
  <li>Visual Studio Professional 2013</li>
  <li>C#</li>
  </ul></li>
  </ul>
  
  <p>I'm working on an app which communicates with a device, a regulated power supply, which controls output voltage to set or get voltage to or from it.
  For example, if the current voltage is 0V and 10V is set to the device, it will try to change the output voltage
  to the input one, 10V. If you read the voltage from the device then, you can see it gradually goes up over time,
  like 0V, 1V, 2V,... 8V, 9V, 10V. The app also shows the time course of the voltage on a chart.</p>
  
  <p>I've written a code to implement the functions. There's a while loop in the code to get voltage and show it on a chart continuously
  so I used asynchronous programming with async/await to enhance responsiveness. Here is the simplified version of the actual code.</p>
  
  <pre><code>private bool loop_flag = true;
  private System.IO.Ports.SerialPort sp = new SerialPort();
  private async Task measurement()
  {
      Open_serial_port();
  
      loop_flag = true;
      var sw = new Stopwatch();
      while (loop_flag)
      {
          double voltage = await Task.Run(() =&gt; Get_voltage_from_device());
          Update_chart(sw.ElapsedMilliseconds, voltage); // this updates a chart control showing the time course of the value.
      }
      sw.Stop();
      Close_serial_port();
  }
  
  private double Get_voltage_from_device()
  {
      return Parse_bytes_into_voltage(Exec_command("get voltage"));
  }
  
  private void button_set_voltage_Click(object sender, EventArgs e)
  {
      Exec_command("set voltage " + textBox_voltage.Text);
  }
  
  private void button_stop_Click(object sender, EventArgs e)
  {
      loop_flag = false;
  }
  
  private byte[] Exec_command(string command)
  {
      sp.DiscardInBuffer();
      sp.DiscardOutBuffer();
      Send_command_using_serialport(command); // uses SerialPort.Write() method
  
      var received_data_raw = new List&lt;byte&gt;();
      var sw = new Stopwatch();
      sw.Start();
      // since data from the device may be received in several parts 
      // getting data repeatedly using while loop is necessary
      // this loop usually takes about 20 msec to finish
      while (true) 
      {
          if (sw.ElapsedMilliseconds &gt; 1000) // 1000 can be anything not too large or small.
          {
              throw new TimeoutException();
          }
  
          if (sp.BytesToRead == 0) // the buffer is often empty
              continue;
  
          while (sp.BytesToRead &gt; 0)
          {
              received_data_raw.Add((byte)sp.ReadByte());
          }
  
          if (received_data_raw.Count == 1 &amp;&amp; received_data_raw.ToArray()[0] == 0xFF) // 0xFF means the voltage was set successfully.
              break;
  
          if (received_data_raw.Count == 2) // all the data for voltage were received
              break;
      }
      sw.Stop();
  
      return received_data_raw.ToArray();
  }
  </code></pre>
  
  <p>However, I encountered one issue. 
  When the command to fetch voltage is sent to the device and the program is waiting for a reply, 
  if a new command to set voltage to the device is sent, the device can't process the message properly and send back 
  a gibberish byte array. It seems like a specification of the device so it can't be changed.</p>
  
  <p>To avoid the issue, the methods to send commands, which are asynchronous, should run in a single thread and 
  be processed one by one. However, googling and searching on StackOverflow gave me no useful information. 
  What should I do to achieve this? Thanks in advance.</p>