title: Updating UI using dispatcher.beginInvoke
body: <p>So, I am reading a message through the mobile Bluetooth and I am trying to update the UI with the data I received.</p>
  
  <p>Calling another method, the 'update' method as following</p>
  
  <pre><code>try
  {
     await update();
  }
  catch (Exception e)
  {
       Debug.WriteLine("Exception has been called");
       Debug.WriteLine("Type| " + e.GetType());
       Debug.WriteLine("Inner Exception| " + e.InnerException);
       Debug.WriteLine("Message| " + e.Message);
  }
  
  and the update method goes as followes
  
  public async Task&lt;String&gt; update()
  {
      string[] sMsg = msg.Split(new Char[]{'\n'});
  
      string[] finalmsg = sMsg[0].Split(new Char[]{':'});
  
      if ((finalmsg.Length != 8 &amp;&amp; sMsg[0] == msg) || sMsg.Length &lt; 2)
      { // not finished reading 
          //Debug.WriteLine("\n\n--skip as the size is : " + finalmsg.Length);
          return null;
      }
  
      msg = msg.Substring(sMsg[0].Length+2,msg.Length-(sMsg[0].Length+2));
      if (finalmsg.Length &gt;= 8){
  
          System.Threading.Thread thread = new System.Threading.Thread(
              new System.Threading.ThreadStart(
                     delegate()
                     {
                       Dispatcher.BeginInvoke(delegate()
                       {
                          string[] fm = finalmsg;
                          RX.Text = finalmsg[0];
                          RY.Text = finalmsg[1];
                          LX.Text = finalmsg[2];
                          LY.Text = finalmsg[3];
                          RF1.Text = finalmsg[4];
                          RF2.Text = finalmsg[5];
                          LF1.Text = finalmsg[6];
                          LF2.Text = finalmsg[7];
                       });
                    }
              ));
  
          thread.Start();
  
          if(!thread.IsAlive)
              thread.Abort();
          }
  
      return finalmsg[0];
      }            
  }
  </code></pre>
  
  <p>So the problem I am facing now is that, updating the UI in the body of Dispatcher.BeginInvoke works for 20 seconds and then an exception fires as following</p>
  
  <pre><code>exception has been thrown by the target of an invocation
  </code></pre>
  
  <p>The exception is fired in the App.xaml, Application_UnhandledException method
  That means the exception is not handled in the code, and I was not able to catch even with try clause.</p>
  
  <p>What I am thinking now is I am doing a mistake because I don't really understand how dispatcher.beginInvoke</p>
  
  <p>So could you please advice of what may went wrong here ?
  Thanks in advance.</p>
  
  <p>Edit:
  To elaborate more this is the stack trace of the exception I got</p>
  
  <p>Stack Trace:</p>
  
  <pre><code>  at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
    at System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
    at System.Delegate.DynamicInvokeImpl(Object[] args)
    at System.Windows.Threading.DispatcherOperation.Invoke()
    at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
    at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
    at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
    at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam* pParams, ScriptParam* pResult)
  </code></pre>