title: Return a Task from a method with type void
body: <p>I would like to create a task to run serial commands on. At this time I do not need to return anything from the method that is doing the work. This will probably change later, but I am now curious as to how this.</p>
  
  <p>This is what I have. I would like to use a separate method for the task instead of creating an anonymous action. I have tried returning void, with the result of "void can not be explicitly converted to a Task". I have also tried. <code>Task&lt;void&gt;</code>. The Last thing I have tried is returning a Task, but I receive, error "Not all Code paths return a value" and "Can not implicily convert void to type task"</p>
  
  <p><a href="https://i.stack.imgur.com/PWLB3.png" rel="noreferrer"><img src="https://i.stack.imgur.com/PWLB3.png" alt="enter image description here"></a></p>
  
  <p>In the pass I have used a Thread to accomplish this, but I'd like to use Tasks this time around.</p>
  
  <pre><code>internal class Hardware
  {
      private EventHandler&lt;SequenceDoneEventArgs&gt; SequenceDone;
  
      private List&lt;Step&gt; Steps;
      private System.IO.Ports.SerialPort comport = null;
  
      private Task SequenceTask;
      private CancellationTokenSource RequestStopSource;
      private CancellationToken RequestStopToken;
  
  
      private void Initialize()
      {
          comport = new System.IO.Ports.SerialPort("COM2", 115200, System.IO.Ports.Parity.None,8);
          comport.DataReceived += Comport_DataReceived;
      }
  
      public async void RunSequence()
      {
          if (comport == null)
          {
              Initialize();
          }
          if (!comport.IsOpen)
          {
              comport.Open();
          }
  
          RequestStopSource = new CancellationTokenSource();
          RequestStopToken = RequestStopSource.Token;
  
          SequenceTask = await Task.Run(() =&gt; { doSequence(); });
  
      }
  
      private Task doSequence()
      {
          //** Run Sequence stuff here
  
      }
  }
  </code></pre>
  
  <p>ETA:</p>
  
  <p>In the end this is my the complete solution</p>
  
  <pre><code>internal class Hardware
  {
      private EventHandler&lt;SequenceDoneEventArgs&gt; SequenceDone;
  
      private List&lt;Step&gt; Steps;
      private System.IO.Ports.SerialPort comport = null;
  
      private Task SequenceTask;
      private CancellationTokenSource RequestStopSource;
      private CancellationToken RequestStopToken;
  
  
      private void Initialize()
      {
          comport = new System.IO.Ports.SerialPort("COM2", 115200, System.IO.Ports.Parity.None,8);
          comport.DataReceived += Comport_DataReceived;
      }
  
      public async void RunSequence()
      {
          if (comport == null)
          {
              Initialize();
          }
          if (!comport.IsOpen)
          {
              comport.Open();
          }
  
          RequestStopSource = new CancellationTokenSource();
          RequestStopToken = RequestStopSource.Token;
  
          SequenceTask = await Task.Factory.StartNew(async () =&gt; { await doSequence(); });
  
      }
  
      private Task doSequence()
      {
          //** Run Sequence stuff here
  
          //return null;
          return Task.CompletedTask;
      }
  }
  </code></pre>