none
Controls created on one thread cannot be parented to a control on a different thread

    Question

  • HI

    I have label in a class.

    Class car
    {
       ...
       public Label labelCar


    }

    I create an object of that class in Windows forms file.

    car CarBMW = new car();

    When i try to add the corresponding label (panel.Controls.Add(car.labelCar)) it gives the next error:

    "Controls created on one thread cannot be parented to a control on a different thread"

    I saw the msdn example with control.invoke() but it seems short for understanding.

    Can you give good example or explanation about this subject

    Regards

    Luis


    Monday, June 22, 2009 5:01 PM

Answers

  • Because of how the CLR works, object references defined within one Application Domain, Thread, or Process have no meaning in another Domain, Thread or Process.  Every application basically sees a machine model that has no other applications running.  It has its' own virtual memory space to run that is the size of the actual memory space allowed by the machine's OS and hardware.

    In order to access those other objects, the data must be passed---or marshalled---across unmanaged memory.  Sometimes to and from it, but usually across the virtual machines that each thread sees.


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by elvinny Monday, June 22, 2009 5:33 PM
    Monday, June 22, 2009 5:16 PM
  • Well, when you have a UI control that's created on another thread other than the UI thread, you're not allowed to attach that particular control to the current thread.  WinForms simply won't allow it. 

    That being said, Car seems to be a business object, while Label is a UI object.  As far as design is concerned, you probably shouldn't be defining UI elements within business objects. That's a design no-no. 

    If you give us more of an example of what you're trying to do, perhaps we could help better.
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marked as answer by elvinny Monday, June 22, 2009 5:33 PM
    Monday, June 22, 2009 5:17 PM

All replies

  • Because of how the CLR works, object references defined within one Application Domain, Thread, or Process have no meaning in another Domain, Thread or Process.  Every application basically sees a machine model that has no other applications running.  It has its' own virtual memory space to run that is the size of the actual memory space allowed by the machine's OS and hardware.

    In order to access those other objects, the data must be passed---or marshalled---across unmanaged memory.  Sometimes to and from it, but usually across the virtual machines that each thread sees.


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by elvinny Monday, June 22, 2009 5:33 PM
    Monday, June 22, 2009 5:16 PM
  • Well, when you have a UI control that's created on another thread other than the UI thread, you're not allowed to attach that particular control to the current thread.  WinForms simply won't allow it. 

    That being said, Car seems to be a business object, while Label is a UI object.  As far as design is concerned, you probably shouldn't be defining UI elements within business objects. That's a design no-no. 

    If you give us more of an example of what you're trying to do, perhaps we could help better.
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Marked as answer by elvinny Monday, June 22, 2009 5:33 PM
    Monday, June 22, 2009 5:17 PM