Click here to Skip to main content
15,904,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hai i am geting the error Invalid cross thread access: I am Using Silverlight 4 & rest Serives, Where i done wrong please help Me .Below is my code.
C#
private  void GetResponseCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
    Stream streamResponse = response.GetResponseStream();
    StreamReader streamRead = new StreamReader(streamResponse);
    string responseString = streamRead.ReadToEnd();
    MemoryStream ResMs = new MemoryStream(Encoding.UTF8.GetBytes(responseString));
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Pos_Silverlight.DataContract.Response));
    Pos_Silverlight.DataContract.Response _ResLogIn = (Pos_Silverlight.DataContract.Response)ser.ReadObject(ResMs);
    if (_ResLogIn.Code == "1")
    {
        //I am getting Error Hear Only
        Home obj = new Home();
        this.Content = obj;
    }
    else
    {
        MessageBox.Show("InValied UserName and PassWord");
        return;
    }
    streamResponse.Close();
    streamRead.Close();
    response.Close();
}
Posted
Updated 28-May-13 20:43pm
v2

I had faced this Exception before and the below code helpd me:

C#
Invoke(new MethodInvoker(delegate {
    // Your code
}));


I hope it help you too.
 
Share this answer
 
v3
Hi,

Maysam Tolouee's ans is also valid but I think as you using IAsyncResult

then use
C#
BeginInvoke and EndInvoke
 
Share this answer
 
You are Setting the Content property from another (not the UI) thread. You have to marshall the call by reinvoking on the UI thread as Meysam Tolouee showed you. For this you can make an "inline" delegate like he showed, or a normal method which does only the Content assignment. Btw. I feel this is one of the most common questions here, if this topic is new to you, maybe you want to search on CP for crossthread call problems and see how other solved it...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900