[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting Crooss Thread Exception on updating label....

Posted on 2011-04-26
3
Medium Priority
?
265 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

0
Comment
Question by:Adnan
3 Comments
 
LVL 4

Expert Comment

by:malikirfan28
ID: 35466304
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
 
LVL 11

Accepted Solution

by:
saragani earned 2000 total points
ID: 35468299
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
 

Author Comment

by:Adnan
ID: 35473357
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question