Click here to Skip to main content
Sign Up to vote bad
good
See more: C#WinForm
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 18 Jul '12 - 5:07

Comments
Wes Aday - 18 Jul '12 - 11:17
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:
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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 498
1 Mahesh Bailwal 332
2 Arun Vasu 269
3 Maciej Los 263
4 Aarti Meswania 205
0 Sergey Alexandrovich Kryukov 9,660
1 OriginalGriff 7,329
2 CPallini 3,968
3 Rohan Leuva 3,339
4 Maciej Los 2,851


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 18 Jul 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid