Use of threads
Posted on 2003-12-03
I am a novice to using threads in C#. I have tried it for the first time and am calling a method ConnectionDatagridRefresh()from this thread. This method first creates a connection, then fills a dataset, and finally populates a datagrid control on one of the tab pages of the same form I am creating the thread and calling the function from. The function is also local to the same from. The purpose is to asynchronously run the whole process in a separate thread and allow the user to do some user related GUI work in the mean time without interruption. The problem is that I am getting an error if, during the execution of this thread , I try to click on the datagrid control or some other control on the tab page of a tab control the datagrid control is placed on. The error message is: "Controls created on one thread cannot be parented to a control on a different thread". The program execution breaks at the point in code where I am trying to fill the datagrid control with the data in the dataset.
As far as I can think of based on the error message, the datagrid control I am trying to fill using this thread has been originally created by another inherent thread that is called internally when I run the application. This inherent thread (i guess) takes care of the GUI for the form and all of its controls.
Until now, I could not resolve this problem. Note that the error only pops up if I try to click on the tab page or any of its contained controls including the datagrid control it contains. Otherwise, everything works as normal.
I would really appreciate if someone can help me resolving this problem. If you need further information and code details I can post it too.
Thank you in advance