Troubles with Multithreads and Messaging Center in Xamarin.forms

0

I'm developing another sample where Messaging Center send status messages not coupled from device code to my view models.

At this point I used:

  • A alert message;
  • Label in my view;
  • A method by dependency injection from native code(interfaced and created before).

To notice the events before try in View models... etc

For it I used a static view instance in my share application constructor (App.xaml) where in view constructor I Subscript the status.

App (shared)

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();
            }
        }

(View shared)

public MainView ()
        {
            InitializeComponent ();

            string a="Test";
            #region MessegeCenter

            MessagingCenter.Subscribe<string,string>("APP", "Message_Received", async (sender,arg) => 
            {
                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

        }

Into the specific platform code (Device - UWP):

  • I create a timer that sends messages after some time instanced in mainpage constructor.
  • A HID device that notice me when some msg comes from USB.

The dispatcherTimer

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 > timesToTick)
             {                   
                 MessagingCenter.Send<string,string>("APP","Message_Received","MR");
             }
         }

The HIDInit and HID InputReport event

        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<string,string>("App","Message_Received","MR");

        }

When I run any case with Dispatchertimer "works".

When I run by the Hidinputreport event with the alertmessage creates a system.exception in alertmessege line.

This is the "System.Exception"

if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
            };
endif

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.

System.Exception: 'The application call a marshalled interface for another thread.

 (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))'

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 ...

Can I call it into the sender into Messeging Center Subscripte ?

How can I correct the other troubles with Threads ? Manual Reset Events ? EventWaitHandle ? (Inheritance:Object->MarshalByRefObject->WaitHandle->EventWaitHandle) ... so invasive way :/

I'm sorry if I ask some stupid question or show stupit code here ... but I don't know how to organize it WELL

Thank you in advance

Guilherme

c#
multithreading
xamarin.forms
uwp
thread-safety
asked on Stack Overflow Nov 14, 2018 by Guilherme Marques • edited Nov 14, 2018 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0