Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Threading question...

JPHochbaumJPHochbaum USMember ✭✭✭
edited April 2017 in Xamarin.Forms

Since I can't use System.Threading.Thread I am struggling to find a way wait for a message from a server to populate a List, after I send a message, as I lay out below.

Before I get to my for each loop I would like to perform some kind of wait operation, for the List<messageList> to get populated, that doesn't freeze the UI.

I am not that great at threading as of yet, but I understand the general concept.

 DependencyService.Get<IGetNativeData>().SendServerMessage(message);


                    if (picker.Items.Count == 0)
                    {
                        foreach (string messageListItem in this.messageList)
                        {
                            picker.Items.Add(messageListItem);

                        }

                    }

I am using event driven methods to populate the messageList<>:

private void OnMessageReceived(string msg)
        {
            if (msg.StartsWith("F"))
            {
                if (this.filterForm != null)
                {
                    this.filterForm.ProcessIncomingFilterMsg(msg);
                }
            }
            else
            {

                Device.BeginInvokeOnMainThread(() =>
                {
                    this.ProcessIncomingMessage(msg);

                });
            }

        }

        private void ProcessIncomingMessage(string msg)
        {
            //process the message
            if (msg != "")
            {
                MainViewRowViewModel model = new MainViewRowViewModel(this.ParseMessage(msg));

                dataView.ItemsSource = MainViewRowViewModel.RowPairs;

            }

            this.CallInterface();
        }

Best Answer

Answers

  • JPHochbaumJPHochbaum USMember ✭✭✭

    I am familiar with async/await. So maybe this isn't really a threading issue but a logic issue?

    Here is some pseudo code:

    SendMessageToServer(messageToSend);
    
    //Need a condition here to see if message is received and parsed (which happens in a separate method and populates a global List<>).  Would like to avoid a while loop, which I am currently using and it is crushing my UI.
    
    DoStuffWithMessage(this.messageReceivedList);
    

    I may need a code sample if you have time.

  • JPHochbaumJPHochbaum USMember ✭✭✭
    edited April 2017

    Quick question on await Task.Delay

     if(this.messageList.Count == 0)
                        {
                            await Task.Delay(-1);
                        }
    

    In the above code snippet does this current task continually run this condition until it is false?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    No. if is not a loop. Execution will take place downward.

    if - this condition is true
    {
         // Do this
    }
    //This code is executed regardless
    
  • JPHochbaumJPHochbaum USMember ✭✭✭

    Thought it was a stupid question, lol

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The only stupid question is the one you have but don't ask.

Sign In or Register to comment.