title: Interrupt calling thread when exception in child task
body: <p>I have the following scenario:</p>
  <p>I have a plugin class (unfortunately synchronous and that's out of my control) which runs automation scripts. At startup, a task is started which runs in the background continuously until the user stops it. As this task runs, it is parsing streaming incoming data from a serial port and continuously updating a list of responses. In parallel, the script will be changing some other values and monitoring how the responses react.</p>
  <p>My problem is this -- it is possible that the parser could throw an exception and that renders any of the remaining script invalid. But, I can't find a good way to interrupt the script (i.e. void Main() below) should this parallel task throw. If I wait until my script is done, and then await the task, I do get the exception, but by then there is potentially a lot of time wasted. So I'm looking for better ways to interrupt the script that is running should the parallel task error. I've tried posting on the initial synchronization context but with no luck as well. I've parred down an example below to simulate a similar scenario</p>
  <pre><code>public class Test : TestPlugin
  {
      Task _t;
      List&lt;string&gt; data = new List();
  
      public Test(){ }
      
      public override void Cleanup()
      {
          //_t.Wait();
      }
      
      public override void Main()
      {
          // i want this to be interrupted if the polling task started in startup throws. 
          //Right here simulates running test steps, 
          //any of which can access the data that's been updated by the &quot;background&quot; task at any time
          try
          {
              int i = 0;
              while (i &lt; 10)
              {
                  Task.Delay(1000).Wait();
                  Console.WriteLine(&quot;step&quot; + i + &quot;Latest: &quot; + data.latest());
                  i++;
              }
          }
          catch(Exception ex)
          {
              Console.WriteLine(&quot;EXCEPTION&quot;);
          }
      }
  
      public override void Setup()
      {
          _t = Task.Run(async () =&gt;
          {
              int i =0;
              while (i &lt; 10)
              {
                  await Task.Delay(200);
                  Console.WriteLine(i);
                  i++;
                  //i would parse incoming data and add to array here
                  data.add(i);
                  if (i &gt; 3) throw new Exception(&quot;UH OH&quot;);
              }
          });
      }
  }
  </code></pre>