views:

52

answers:

2

Here is my code scenario,

In main thread I create panel(s) (based on some configuration), and then in the next step based on the file type (like video, ppt, image...) I create appropriate controls (dynamically) to show on the forms.

The creation of the control will be done in separate threads (for each file). After creating the control, it throws an error when I try to add that control to the panel (which was created in the main thread), something like cross thread violation, control was accessed from one thread other that it was created.

Can some one please help me in this regard?

In this scenario, everything - creating the panels and controls - will be dynamic. Nothing will be static.

I tried some of the articles found here on StackOverflow (like, control.BeginInvoke() .. etc), but wasn't able to solve my problem.

+3  A: 

You cannot create controls in another thread and add them to a form that was created in the main UI thread. Windows requires that the child windows owned by a top-level window belong to the same thread. You have to create them in the UI thread. Leverage the Control.Begin/Invoke() method.

Hans Passant
Thanks for your response, but my actual problem is.. Let say, On my form we have 3 panels, on 1st panel I show some collection of images, in 2nd panel some video and in 3rd panel show some Scrolling text. When images in first panel changes, the scrolling text is halted(jerks) for some time.This is because all the 3 controls and the timer which changes the images in picture box were created in same thread(i.e. main thread.)
N Chary
@N Chary: Is *what*?
Adam Robinson
Check this thread for ways to improve drawing behavior: http://stackoverflow.com/questions/2612487/how-to-fix-the-flickering-in-user-controls/2613272#2613272
Hans Passant
the above mentioned problem is one of other problems.. Here is another issue, let say in 1st panel we show images, in 2nd panel some powerpoint presentation. It takes some time (1 or 2 sec) to completely load the ppt in WebBrowser control. Other controls (image rotator in this case) were being halted till the ppt loads completley. Because of all these controls(webbrowser, timer which changes the images, etc...) are in single thread(main thread).
N Chary
The kind of magic you are looking for is not available. Use a splash screen or an animation to paper it over.
Hans Passant
A: 

Controls should only be created and accessed from the main GUI thread.

You can do work in other threads, but do not access GUI components from other threads.

Andreas Paulsson