title: Troubles with Multithreads and Messaging Center in Xamarin.forms
body: <p>I'm developing another sample where Messaging Center send status messages not coupled from device code to my view models.</p>
  
  <p>At this point I used:</p>
  
  <ul>
  <li>A alert message;</li>
  <li>Label in my view;</li>
  <li>A method by dependency injection from native code(interfaced and created before).</li>
  </ul>
  
  <p>To notice the events before try in View models... etc</p>
  
  <p>For it I used a static view instance in my share application constructor (App.xaml) where in view constructor I Subscript the status.</p>
  
  <p>App (shared)</p>
  
  <pre><code>public partial class App : Application
      {
          public static ConnectViewModel CVM { get; set; }// Connection View Model
          #region MasterDetailPage
          public static MasterDetailPage MDP;
          public static NavigationPage NAV = null;
          public static MainView _mainpage;
          #endregion
  
          public App ()
          {
              InitializeComponent();
              InitializeApplication();
              NAV = new NavigationPage(new StarterView()) { BarBackgroundColor = Color.FromHex("701424"), BarTextColor = Color.White }; ;
              MDP = new MasterDetailPage();
              MDP.BackgroundColor = Xamarin.Forms.Color.FromHex("701424");
              _mainpage = new MainView();
              MDP.Master = _mainpage;
              MDP.Detail = NAV;
              MainPage = MDP;
              MainPage.Title = "H2X";
  
          }
   private void InitializeApplication()
          {
  
              if (CVM == null)
              {
                  CVM = new ConnectViewModel();
              }
          }
  </code></pre>
  
  <p>(View shared)</p>
  
  <pre><code>public MainView ()
          {
              InitializeComponent ();
  
              string a="Test";
              #region MessegeCenter
  
              MessagingCenter.Subscribe&lt;string,string&gt;("APP", "Message_Received", async (sender,arg) =&gt; 
              {
                  string b = a;
                  a = $"{arg}";
  
                  try
                  { 
                 * await DisplayAlert(App.BM_Status, "Ok", "OK");*
                  }catch(Exception e)
                  {
                  string a = e.Message;
                  }
                 * generic_label_of_my_view = generic_label_of_my_view + "+";//It's not async one*
                  *string test = App.CVM.All_conn.Msg_Reciever();//Injection - It's not async one*
  
              });
              #endregion
  
          }
  </code></pre>
  
  <p>Into the specific platform  code (Device - UWP):</p>
  
  <ul>
  <li>I create a timer that sends messages after some time instanced in mainpage constructor.</li>
  <li>A HID device that notice me when some msg comes from USB.</li>
  </ul>
  
  <p>The dispatcherTimer</p>
  
  <pre><code>void dispatcherTimer_Tick(object sender, object e)
           {
               DateTimeOffset time = DateTimeOffset.Now;
               TimeSpan span = time - lastTime;
               lastTime = time;
               //Time since last tick should be very very close to Interval
               TimerLog.Text += timesTicked + "\t time since last tick: " + span.ToString() + "\n";
               timesTicked++;
               if (timesTicked &gt; timesToTick)
               {                   
                   MessagingCenter.Send&lt;string,string&gt;("APP","Message_Received","MR");
               }
           }
  </code></pre>
  
  <p>The HIDInit and HID InputReport event</p>
  
  <pre><code>        public async void HID_Init()
          {
  
  
              var selector = HidDevice.GetDeviceSelector(a_Id, b_Id, c_ID, d_ID);
              var devices = await DeviceInformation.FindAllAsync(selector);
  
              if (devices.Any())
              {
                  // At this point the device is available to communicate with
                  // So we can send/receive HID reports from it generically
  
  
                  console_text = "HID devices found: " + devices.Count;
  
                  device = await HidDevice.FromIdAsync(devices.ElementAt(0).Id, FileAccessMode.ReadWrite);
  
                  if (device != null)
                  {
                      // At this point the device is available to communicate with
                      // create my input caller/event
                      device.InputReportReceived += inputReportReceived;//invoke caller
  
                      deviceWatcher = DeviceInformation.CreateWatcher(selector);
                      deviceWatcher.Removed += deviceRemovedEventHandler;//checa se nada foi removido
                      deviceWatcher.Start();
                  }
                  else
                  {
                      // There were no HID devices that met the selector criteria
                      throw new Exception("MUTT HID device not found");
                  }
  
              }
              else
              {
                  // There were no HID devices that met the selector criteria
                  console_text = "HID device not found";
              }
          }
  
          private void inputReportReceived(HidDevice sender, HidInputReportReceivedEventArgs args)
          {
              var bbytes = new byte[10];
              wait_streaming = true;
              DataReader dataReader = DataReader.FromBuffer(args.Report.Data);
              dataReader.ReadBytes(bbytes);
  
              console_text += System.Text.Encoding.ASCII.GetString(bbytes, 2, bbytes[1]);
  
              is_read = false;
  
              wait_streaming = false;
  
              MessagingCenter.Send&lt;string,string&gt;("App","Message_Received","MR");
  
          }
  </code></pre>
  
  <p>When I run any case with Dispatchertimer "works".</p>
  
  <p>When I run by the Hidinputreport event with the alertmessage creates a system.exception in alertmessege line.</p>
  
  <p>This is the "System.Exception"</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();
              };
  endif
  </code></pre>
  
  <p>When I run by the Hidinputreport event with the Label a marshalled interface crash with other thread in my call from messegingCenter in native code.</p>
  
  <pre><code>System.Exception: 'The application call a marshalled interface for another thread.
  
   (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))'
  </code></pre>
  
  <p>When I run the injection, works but I'm afraid that this Thread troubles make some semantical bug in my project cause I need to call INofifyPropertyChanged in shared code to print in my view the message but ... </p>
  
  <p>Can I call it into the sender into Messeging Center Subscripte ?</p>
  
  <p>How can I correct the other troubles with Threads ? Manual Reset Events ? EventWaitHandle ? (Inheritance:Object-><strong>MarshalByRefObject</strong>->WaitHandle->EventWaitHandle) ... so invasive way :/</p>
  
  <p>I'm sorry if I ask some stupid question or show stupit code here ... but I don't know how to organize it WELL</p>
  
  <p>Thank you in advance</p>
  
  <p>Guilherme</p>