Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a Main Form it contains a dock panel in which am loading the child forms.
I am getting the child form instance using reflection, where it takes time and freezes the Application. I have tried threading where i have created the instance using background worker and also normal threading. Time taken is due to fetching data from web services as well as binding the controls.

I am getting exception. Control created on one thread cannot be accessed on a control in different thread.

Can any one help me out.

Thanks in advance
Posted

You have to use invoke from within other threads to control the ui elements. Have a look at the link

http://www.yoda.arachsys.com/csharp/threads/winforms.shtm[^]

Good luck!
 
Share this answer
 
What you are getting is a cross threading exception.

When you have a wo UI in two different threads, you cannot simply interchange data between them. You need to use delegate to interchage data.

For example If I have a Form A in Thread 1 and there I need data in another thread that I want to populate in Form A, I need to first create a delegate pointing to thread 2 and then use this.invoke to access that data.

Let me know if you need a code snippet.
 
Share this answer
 
Hi,

Generally, one cannot access a form's control through a Thread. To access the form controls, use Delegates and try to do your process. This might solve your problem.

Regards,
Suresh
 
Share this answer
 
Yes,

this is basically because of Thread affinity for each control. You cannot build a control any other thread than UI Thread.

so expose a method which only calls for data and use Reflection to load the form in UI thread. as there would be no web service call, the UI Will load quickly. Now create a separate thread and call the exposed method which might load the data for the form.

When the data is loaded correctly, you can bind the data to the UI Elements.
:thumbsup:
 
Share this answer
 

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