We help IT Professionals succeed at work.

Getting Crooss Thread Exception on updating label....

Adnan
Adnan asked
on
294 Views
Last Modified: 2012-05-11
hi iam Getting Crooss Thread Exception on updating label....it happen when value changes on label....i have to create delegate event but somewhere iam stucked and icant figure out how to fix this... :? :(
private void ResetClientControll()
        {
            myOperator.CurrentMode = OperatorMode.Client;

            ClientInformation cliInfo = AmEngine.GetEngagementInformation(myOperator).GetClientInformation(new ClientId(myOperator.CurrentClient_ID));

            LabelSelectedClient = cliInfo.ClientName;
            LabelTotalOpenTransactions = cliInfo.CountOpenTransactions;  GETTING EXCEPTION ON THIS LINE::::::::::


            decimal diff= 0;
            decimal cliInfDiff = cliInfo.Diff;
            if (cliInfDiff != (decimal)diff)
            {
                lblTotalDifference.Visible = true;
                lblTotDiff.Visible = true;
                LabelTotalDifference = cliInfDiff;
            }
            else
            {
                lblTotalDifference.Visible = false;
                lblTotDiff.Visible = false;
            }
        }



The label is a Long...

public long LabelTotalOpenTransactions
        {
            get 
            {
                return Convert.ToInt32(lblTotalOpenTran.Text);
            }
            set 
            { 
                lblTotalOpenTran.Text = Convert.ToInt32(value).ToString(); 
}
        }

Open in new window

Comment
Watch Question

you are getting this exception becuase you are accessing label which UI thread owns from another thread. So you will need to make thread safe call as described here


http://msdn.microsoft.com/en-us/library/ms171728.aspx
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Oki..i will try your code to see if it works but i resolved it this way code below...
delegate void SetSensorValueDelegate(long value);
        private void SetSensorValue(long value)
        {
            if (lblTotalOpenTran.InvokeRequired)
            {
                SetSensorValueDelegate del = new SetSensorValueDelegate(SetSensorValue);
                lblTotalOpenTran.Invoke(del, new object[] { value });
            }
            else
            {
                LabelTotalOpenTransactions = value;
            }
        }

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.