Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I Am using workder thread in my application. I want that when my application raise any exception it should be populated in the listbox and flow of exectution shouldn't be breaked out. But it showing this exception.

Cross-thread operation not valid: Control 'txtBxException' accessed from a thread other than the thread it was created on.


Plz guide
Posted
Comments
[no name] 18-Jul-12 11:17am    
Use Invoke or use a delegate to access the control

1 solution

This is common error when one starts with WinForms programming.
You need to do something like this:
C#
if (txtBxException.InvokeRequired)
        txtBxException.Invoke(new MethodInvoker(
            delegate
            {
                txtBxException.Text = "Describe error here";
            }
            ));
    else
    {
        txtBxException.Text = "Describe error here";
    }

The problem there is limitation in WinForms that controls can be updated only from main (GUI) thread, on which they were originally created.
So, when you're working on background thread you need to deffer updating control to main thread using .Invoke() or .BeginInvoke() methods.
 
Share this answer
 
v2

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