Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
IT showing error is Invalid cross-thread access in Silverlight app Please Help Me, Where i done the mistack Please
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
ReqInvoiceHeader _ReqInvoice = new ReqInvoiceHeader();
_ReqInvoice.CardCode = strCmCode.Trim();
_ReqInvoice.CardName = strCmName.Trim();
_ReqInvoice.Remarks = srtRemarks.Trim();
ReqInvoiceLines oInvLine = new ReqInvoiceLines();
for (int i = 0; i < 2; i++)
{
//Hear is the Error
//Invalid cross-thread access
oInvLine.ItemCode = ((TextBlock)DataGridItem.Columns[0].GetCellContent(DataGridItem.SelectedItem)).Text;
oInvLine.Quantity = ((TextBlock)DataGridItem.Columns[1].GetCellContent(DataGridItem.SelectedItem)).Text;
oInvLine.UnitPrice = ((TextBlock)DataGridItem.Columns[2].GetCellContent(DataGridItem.SelectedItem)).Text;
 
}
_ReqInvoice.Details.Add(oInvLine);
MemoryStream ReqMs = new MemoryStream();
DataContractJsonSerializer jsondata = new DataContractJsonSerializer(typeof(ReqLogIn));
jsondata.WriteObject(ReqMs, _ReqInvoice);
ReqMs.Position = 0;
StreamReader sr = new StreamReader(ReqMs);
string postData = sr.ReadToEnd();
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
ReqInvoiceLines cLASS
namespace Pos_Silverlight.DataContract
{
    [DataContract(Name = "ReqInvoiceLines")]
    public class ReqInvoiceLines
    {
        [DataMember(Name = "Product")]
        public string ItemCode { get; set; }
 
        [DataMember(Name = "Qty")]
        public string Quantity { get; set; }
 
        [DataMember(Name = "UnitPrice")]
        public string UnitPrice { get; set; }
        public ReqInvoiceLines()
        {
        }
        public ReqInvoiceLines(string Product, string Qty, string UnitPrice)
        {
            this.UnitPrice = UnitPrice;
            this.Quantity = Qty;
            this.ItemCode=ItemCode;
        }
    }
    public class ItemsCollection : Collection<ReqInvoiceLines>
    {
        public ItemsCollection()
        {
            Add(new ReqInvoiceLines("hb", "Item Name1", "1"));
            Add(new ReqInvoiceLines("del", "Item Name2", "2"));
            Add(new ReqInvoiceLines("con", "Item Name3", "3"));
 
        }
    }
Posted
Updated 26-May-13 17:14pm
v6

1 solution

Since this is a callbak function, the code calling your code is usually done on a thread other than the UI thread.

You cannot touch any UI controls from anything other than the UI (or startup) thread. You have to Invoke methods on the UI thread to update the controls you want to touch.

There are tons of example on the web on how to do that.
 
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