无效的跨线程访问 [英] Invalid cross thread access

查看:71
本文介绍了无效的跨线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai我发现错误无效的跨线程访问:我正在使用Silverlight 4&休息Serives,我做错了请帮助我.Below是我的代码。

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.

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

推荐答案

我之前遇到过这个例外,下面的代码帮助了我:



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

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





我希望它对你也有所帮助。



I hope it help you too.






Maysam Tolouee'的ans也有效,但我认为当你使用IAsyncResult



然后使用
Hi,

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

then use
BeginInvoke and EndInvoke


您正在从另一个(不是UI)线程设置Content属性。如Meysam Tolouee向您展示的那样,您必须通过重新调用UI线程来编组调用。为此,您可以像他所展示的那样创建一个内联委托,或者只进行内容分配的常规方法。顺便说一句。我觉得这是这里最常见的问题之一,如果这个话题对你来说是新的,也许你想在CP上搜索交叉呼叫问题,看看其他人是如何解决的......
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...


这篇关于无效的跨线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆