UWP Windows 10 app crashes in release mode but works fine in debug mode
<p>My UWP app is crashing in Release mode and works fine in Debug mode but I can't put my finger on what the issue is but I know it's related to a combination of raising an event from System.Threading.Timer and MVVMLight.</p>

<p>I created an new dummy application and use the same code (ZXing.net.mobile where I used 2 portable libraries and I used my own user control which is a simplified version of theirs - I'm using events instead of <code>&lt;Action&gt;</code>). This works fine and I'm currently trying to put more steps into it i.e. include mvvmlight and navigation but so far, I can't reproduce the problem in this dummy app.</p>

<p>The error I'm getting is:</p>

<pre><code>Unhandled exception at 0x58C1AF0B (mrt100_app.dll) in Company.MyApp.App.exe:
0xC0000602:  A fail fast exception occurred. Exception handlers will not be
 invoked and the process will be terminated immediately.
</code></pre>

<p>Followed by:</p>

<pre><code>Unhandled exception at 0x0107D201 (SharedLibrary.dll) in
Company.MyApp.App.exe: 0x00001007.
</code></pre>

<p>When looking at the Threads window, one of the worker thread has the following information if that's of help.</p>

<pre><code>Not Flagged &gt;   4012    0   Worker Thread   &lt;No Name&gt;
    System.Private.Interop.dll!System.Runtime.InteropServices.
    ExceptionHelpers.ReportUnhandledError   Normal
    [External Code]  
     System.Private.Interop.dll!System.Runtime.InteropServices.ExceptionHelpers.
     ReportUnhandledError(System.Exception e) Line 885   
     System.Private.Interop.dll!Internal.Interop.InteropCallbacks.ReportUnhandledError
     (System.Exception ex) Line 17   
     System.Private.WinRTInterop.CoreLib.dll!Internal.WinRT.Interop.WinRTCallbacks.
     ReportUnhandledError(System.Exception ex) Line 274  
     System.Private.CoreLib.dll!System.RuntimeExceptionHelpers.ReportUnhandledException
     (System.Exception exception) Line 152
     System.Private.Threading.dll!System.Threading.Tasks.AwaitTaskContinuation.
     ThrowAsyncIfNecessary(System.Exception exc) Line 784    
     System.Private.Threading.dll!System.Threading.WinRTSynchronizationContext.Invoker.
     InvokeCore() Line 182   
     System.Private.Threading.dll!System.Threading.WinRTSynchronizationContext.Invoker.
     Invoke(object thisObj) Line 162     
     System.Private.CoreLib.dll!System.Action&lt;System.__Canon&gt;.
     InvokeOpenStaticThunk(System.__Canon obj)
     System.Private.WinRTInterop.CoreLib.dll!Internal.WinRT.Interop.WinRTCallbacks.PostToCoreDispatcher.AnonymousMethod__0() Line 266    
     MyCompany.MyApp.App.exe
     MyCompany.MyApp.App.ViewModels.ValidateHandler.Invoke(string pin)
     MyCompany.MyApp.App.McgInterop.dll!McgInterop.ReverseComSharedStubs
     .Proc_(object __this, System.IntPtr __methodPtr) Line 6163
     MyCompany.MyApp.App.McgInterop.dll!Windows.UI.Core.DispatchedHandler__Impl.Vtbl.Invoke__STUB(System.IntPtr pComThis) Line 45147     
    [External Code]  
</code></pre>

<p>Within my QR Code UserControl, it's using a System.Threading.Timer and it raises an event when a QR Code is found:</p>

<pre><code>timerPreview = new Timer(async (state) =&gt;
{
   ....
   // Check if a result was found
   if (result != null &amp;&amp; !string.IsNullOrEmpty(result.Text))
   {
     Debug.WriteLine("Barcode Found: " + result.Text);
     if (!this.ContinuousScanning)
     {
       delay = Timeout.Infinite;
       await StopScanningAsync();
     }
     else
     {
       delay = this.ScanningOptions.DelayBetweenContinuousScans;
     }
     OnBarcodeFound(result.Text);
   }

   timerPreview.Change(delay, Timeout.Infinite);

}, null, 
   this.ScanningOptions.InitialDelayBeforeAnalyzingFrames, 
   Timeout.Infinite);
</code></pre>

<p>In the page that host the QRCode UserControl, I've got the following code:</p>

<pre><code>private async void ScannerControl_BarcodeFound(string barcodeValue)
{
    var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;

    await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&gt; {
        Debug.WriteLine(barcodeValue);
        GetViewModel.BarcodeFound(barcodeValue);
    });
}
</code></pre>

<p>As you can see, I'm passing the QrCode value to my ViewModel and from there, I sent a message and then re-direct to another page:</p>

<pre><code>public void BarcodeFound(string barcodeData)
{
   Messenger.Default.Send&lt;string&gt;(barcodeData, MessengerTokens.QrCodeFound);
   this.NavigationFacade.NavigateToMyOtherPage();
}
</code></pre>

<p>I could keep going and provide additional code, but as I add additional breakpoints I can see that the code and passing the correct value and going to the correct location but eventually it throws this error. If I add additional error handlers or dispatcher code, it eventually works and goes to the next page as expected but when I click on a button in my CommandBar, it then takes a while and it eventually throws the same error, so by adding these additional bits of code, I feel I'm just pushing down the problem further down the line.</p>

<p>Anyone got any suggestions on how I get around this problem. I wish I could share the full app but definitely can't. So I know it will be hard to provide a solution, but if anyone has suggestions I'd appreciate them.</p>

<p>As I said, I think the issue is a result of a combination of threading and mvvmlight but it's driving me nuts that my test app so far is working exactly as expected, so I'm pretty sure it's not Zxing or my UserControl.</p>

<p>Any help, suggestions would be greatly appreciated or if you need me to provide additional info, please let me know what and I'll try to provide it.</p>

<p>Thanks.</p>