title: Native Windows 10 Client App with MobileFirst 8.0 Unhandled Exceptions Error
body: <p>I develop native Windows 10 client LOB apps for a customer.<br>
  The environment: Windows 10 Pro (on the mobile clients / tablets and on the developer box), Visual Studio 2015 Professional with Update 3, all updates and patches installed. The client app uses the most recent „IBM MobileFirst SDK for Windows 8 Universal and Windows 10 Universal platforms“ version 8.0.2016070716  </p>
  
  <p>The application works very well when the application is online and the MobileFirst 8.0 server can be reached.
  We are experiencing application crashes of the client app if the server can not be reached (offline, MFP server stopped). 
  It seems that a worker thread which is started from within the MobileFirst  C# client library throws an exception.  This exception can not be cathed in my code.</p>
  
  <p>Here is some code to demonstrate the problem:</p>
  
  <pre class="lang-js prettyprint-override"><code>private async Task&lt;WorklightAccessToken&gt; ConnectMobileFirst()
  {
      try
      {
          IWorklightClient _client = WorklightClient.CreateInstance();
          WorklightAuthorizationManager _wlauthManager = 
                          _client.AuthorizationManager;
          WorklightAccessToken temp1 = await
                           _wlauthManager.ObtainAccessToken("");
          return temp1;
      }
      catch (Exception exception)
      {
          Debug.WriteLine("Exception occured " + exception.Message);
          return null;
      }
  }
  </code></pre>
  
  <p>Create a new blank app and call the above function from the OnLaunched eventhandler.</p>
  
  <pre class="lang-js prettyprint-override"><code>AccessToken = await ConnectMobileFirst();
  if (AccessToken != null)
  {
      Debug.WriteLine(AccessToken.IsValidToken
          ? "Connection OK, AccessToken is valid"
          : "AccesToken NOT valid");
  }
  </code></pre>
  
  <p>If the MobileFirst Server can be reached everything works as expected and the AccessToken.IsValidToken is true.</p>
  
  <p>If the MobileFirst Server is offline the following happens:</p>
  
  <ul>
  <li>the function <code>ConnectMobileFirst()</code> finishes and a AccessToken object is returned (no exception occurs inside the method)</li>
  <li>the AccessToken.IsValidToken is false (which is correct, since the MobileFirst server is offline).</li>
  <li>Sometimes before and sometimes after the program can execute the <code>Debug.WriteLine</code> statement a other thread hits a NullReferenceException. This „other“ worker thread started somewhere inside the <code>ObtainAccessToken()</code> call inside the IBMMobileFirstPlatformFoundationWin10.dll. The Output window in Visual Studio contains the following information:</li>
  </ul>
  
  <blockquote>
    <p>Exception thrown: 'System.Runtime.InteropServices.COMException' in
    mscorlib.ni.dll WinRT information: Die Serververbindung konnte nicht
    hergestellt werden.</p>
    
    <p>Exception thrown: 'System.Runtime.InteropServices.COMException' in
    mscorlib.ni.dll WinRT information: Die Serververbindung konnte nicht
    hergestellt werden.</p>
    
    <p>'MFPUnhandledExc1.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded
    'D:\VS2015Projects\MFPUnhandledExc1\bin\x86\Debug\AppX\System.Resources.ResourceManager.dll'.
    Module was built without symbols. Exception thrown:
    'System.Net.Http.HttpRequestException' in System.Net.Http.dll
    Exception thrown: 'System.Net.Http.HttpRequestException' in
    mscorlib.ni.dll Exception thrown:
    'System.Net.Http.HttpRequestException' in System.Net.Http.dll
    Exception thrown: 'System.Net.Http.HttpRequestException' in
    mscorlib.ni.dll 'MFPUnhandledExc1.exe' (CoreCLR: CoreCLR_UWP_Domain):
    Loaded
    'D:\VS2015Projects\MFPUnhandledExc1\bin\x86\Debug\AppX\System.Runtime.InteropServices.dll'.
    Cannot find or open the PDB file. Exception thrown:
    'System.Net.Http.HttpRequestException' in mscorlib.ni.dll Exception
    thrown: 'System.Net.WebException' in System.Net.Requests.dll Exception
    thrown: 'System.NullReferenceException' in
    IBMMobileFirstPlatformFoundationWin10.dll</p>
  </blockquote>
  
  <p>If you set a breakpoint on the line after the <code>ConnectMobileFirst()</code> call you can see there is a worker thread still active which has IBMMobileFirstPlatformFoundationWin10.dll in its call stack. This thread is not here anymore, if the backend connection is OK.</p>
  
  <p>This behaviour can also be seen not only if the MobileFirst Server is offline but if the server is slow responding or the network is slow and the server response is big. Also this is not only during connect, but also during <code>WorklightResourceRequest.send()</code>.</p>
  
  <p>My questions:</p>
  
  <ul>
  <li>What can I do to prevent this stray thread from killing my application? (a short term solution)</li>
  <li>Why is there still a worker thread even if the call to ObtainAccessToken() has already finished?</li>
  <li>And why is this thread not handling its exceptions?</li>
  </ul>