locked
Exception Invalid Cross Thread Access While assigning Values to Textbox. RRS feed

  • Question

  • Hello Experts i am developing a silverlight application and deploying in CRM Online. The application is about querying some records using SOAP service and storing it to an ObservableCollection List. From this list i  am assigning the values to Texboxes but everytime i am getting Invalid Cross thread Access Exception. I am doing as below.

    if(personalDetails.Count>0)

    {

     this.Dispatcher.BeginInvoke(() => txtTelephone.Text = (Convert.ToString(Convert.ToDouble(personalDetails[index].telephone))));
                            this.Dispatcher.BeginInvoke(() => txtCity.Text =(Convert.ToString(Convert.ToDouble(personalDetails[index].City))));
                            this.Dispatcher.BeginInvoke(() => txtDistance.Text = Convert.ToString(Convert.ToDouble(personalDetails[index].Distance).ToString("0.00")));

    }

    where  i am wrong.

    Please suggest

    Thank you


    saroj


    Thursday, December 27, 2012 2:08 PM

Answers

  • Hi Andreas thank you for your reply. I debugged my app. What happening is when the method is calling like this.Dispatcher.BeginInvoke(()=>AssignValueToFields()); There is no Exception but the Values are not assigning to textBoxes. all Fields are empty or null. But the values are present in List. Also i have assign values to the textBoxes as u said above. And one more thing is the method is calling two time why i don't know. and in second time the values are assigning. but that should not be like that.

    What should i do.

    Please Suggest.

    Thanks a lot


    saroj


    Friday, December 28, 2012 5:33 AM

All replies

  • Hi Saroj,

    Take a look into this post it will help you to understand how you can update UI in silverlight.

    HTH


    Contact Me
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, December 27, 2012 3:00 PM
    Moderator
  • Hi,

    I use the following statement for updating UI from another thread and it worked for me:

    this.Dispatcher.BeginInvoke(delegate
                    {
                        txt.Text = "ABC";
                    });

    If this doesn't help - debug your app and locate the source code line where the exception is thrown.

    Best regards,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist
    MCPD: SharePoint Developer 2010

    Thursday, December 27, 2012 4:02 PM
  • Hi Andreas thank you for your reply. I debugged my app. What happening is when the method is calling like this.Dispatcher.BeginInvoke(()=>AssignValueToFields()); There is no Exception but the Values are not assigning to textBoxes. all Fields are empty or null. But the values are present in List. Also i have assign values to the textBoxes as u said above. And one more thing is the method is calling two time why i don't know. and in second time the values are assigning. but that should not be like that.

    What should i do.

    Please Suggest.

    Thanks a lot


    saroj


    Friday, December 28, 2012 5:33 AM
  • Hi Expert i was doing wrong so it was not assigning the values. I was calling the Method like this.Dispatcher.BeginInvoke(()=>AssignValueToFields()); and inside method i was assigning values to textBoxes like 

     this.Dispatcher.BeginInvoke(()=>txtTelephone="Value");

    The Method Calling as same and only i replaced this.Dispatcher.BeginInvoke(()=>txtTelephone="Value");  with txtTelephone="Value"; in side the method. It is working fine.


    saroj


    Friday, December 28, 2012 6:59 AM