title: how to safely Opens a thread from InkPresenter.StrokesCollected event? DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
body: <p>the sigsend.AddStrokes(e1) will cause DEBUG &amp;&amp; !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION. the sigend is a inkstrokecontainer class instanc in a UWP app .</p>
  
  <p>below is the code:</p>
  
  <pre><code>     inkpane.InkPresenter.StrokesCollected += rooPage.snink;
            public async void  snink(InkPresenter sender, InkStrokesCollectedEventArgs args) 
   {  task.run(()=&gt;
                  {            sigsend.Clear();
                      sigsend.AddStrokes(e1);
                      var lstream = new MemoryStream();
                      await sigsend.SaveAsync(lstream.AsOutputStream());
                      var siginkbuffer = lstream.ToArray();
                   });
     }
  </code></pre>
  
  <p>the sigend is a inkstrokecontainer.  this will cause  [DEBUG &amp;&amp; !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION]</p>
  
  <p>I have try many method, such as use task.run or  Windows.System.Threading.ThreadPool.RunAsync, to makesure the task is run in background instaed of UI Thread, but is usless.  I also try to use task.run to run this code in a background task, and in the backgroun task use and (CoreDispatcherPriority.Low, async () => and etc to run the   sigsend.AddStrokes(e1) to vist the UI thread. But all of them are useless, it always cause  DEBUG &amp;&amp; !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION  .</p>
  
  <p>The sigsend is not binding to an inkcanva, I just use it to serialize inkstroke in it. </p>
  
  <p>Why? what is the reason?     I think this is some matter in UI Threading. </p>
  
  <p><strong>new:</strong> </p>
  
  <p>I have try some other code, found that seems is the <strong>async</strong> <strong>await</strong> method cause this , if  </p>
  
  <pre><code>  inkpane.InkPresenter.StrokesCollected += rooPage.snink;
                    public **async** void  snink(InkPresenter sender, InkStrokesCollectedEventArgs args)  
  { await task.run(etc.);
  }   
  </code></pre>
  
  <p>will cause an error.</p>
  
  <p>I have to open an new task, because if the workload in the binding method of InkPresenter.StrokesCollected event is too heavy, it also will cause the DEBUG &amp;&amp; !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION. But if I use the await  task.run() and add async to the snink method, it will cause the DEBUG &amp;&amp; !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION. </p>
  
  <p>so, the question is , how to Opens a thread safely ?</p>
  
  <p><strong>new1</strong>  below is simple code, I does not visit UI code in the task.run .ink1 is an inkcanvas UI control in xaml. istring is a json string about 1kb. and run this code, it will cause </p>
  
  <pre><code>#if DEBUG &amp;&amp; !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
              UnhandledException += (sender, e) =&gt;
              {
                  if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
  </code></pre>
  
  <p>below is the simple code.       </p>
  
  <pre><code>public sealed partial class wpage : Page
  
  
   {
          public wpage()
          {
              this.InitializeComponent();
  
              ink1.InkPresenter.StrokeContainer = inkStrokeContainer1;
              ink1.InkPresenter.StrokesCollected += InkPresenter_StrokesCollected;
              ink1.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Pen | CoreInputDeviceTypes.Mouse;
           }
   string istring;
   public async void InkPresenter_StrokesCollected(InkPresenter sender, InkStrokesCollectedEventArgs args)
  
          { 
  
                  await sic(istring);
           }
  
  
  
   public  Task sic (String js)
          {
  
  
         var ts =  Task.Run(async () =&gt;
          { 
  
  
              using (MemoryStream memoryStream1 = new MemoryStream())
              {
                  using (StreamWriter streamWriter1 = new StreamWriter(memoryStream1))
                  {
                      await streamWriter1.WriteAsync(js);
                      await streamWriter1.FlushAsync();
                      using (MemoryStream memoryStream2 = new MemoryStream())
                      {
                          Compressor compressor1 = new Compressor(memoryStream2.AsOutputStream(), CompressAlgorithm.Lzms, 32768);
                          await compressor1.WriteAsync(memoryStream1.ToArray().AsBuffer());
                          await compressor1.FinishAsync();
  
                      }
  
                  }
              }
  
          });
              return ts;
      }
  
  
  }
  </code></pre>