Getting Crooss Thread Exception on updating label....

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

AdnanAsked:
Who is Participating?
 
saraganiConnect With a Mentor Commented:
Hi, as already suggested, you are trying to access and modify properties of a control/UI from a thread which is not the UI thread.

There is a "generic" solution that remove the need to define lots of type of delegates for updating the UI.

Have the following code in the form:

        private void updateUI(MethodInvoker mi)
        {
            if (this.InvokeRequired)
                this.Invoke(mi);
            else
                mi();
        }


And when you are calling ResetClientControll(), call it this way:


MethodInvoker mi = delegate()
{
    ResetClientControll();
};

UpdateUI(mi);


This will cause the whole ResetClientControll() to be called using the UI thread.
0
 
malikirfan28Commented:
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
0
 
AdnanAuthor 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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.