Solved

Use of threads

Posted on 2003-12-03
4
389 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:smkkaleem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 9867988
http://www.codeguru.com/cs_syntax/WorkerThread.html

Pay attention for using Invoke command.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9869492



--- Just a comment, not for points ---




There are probably a dozen different ways you could solve this issue using threads and thread pools (and many of them are less complex than what the above link shows), but I must admit that the link provided above probably shows the most promise while still allowing good programming standards.

While there are less complicated methods available, the power of OO design (Write once, use everywhere) is clearly evident in the sample provided by Alex Farber on page linked by AlexFM.  

I would study this code and adapt to its design strategy.


Of course, if you are looking for something much simpler, you could always create a new virtual/programmatic DataGrid Object and load the data into it and then when it is done copy it to the datagrid that is on your form.  While this method will work (and I have used it), I would not encourage it.  It really is not a good programming practice.



AlexFM...
  Is this code something you've published on code guru (self-advertisement)?   There seems to be a lot of similarities in names and techniques.  As I recall in a past posting you did, you mentioned something about you being a big fan of using delegates (when a lot of other programmers use Interfaces).  Not to mention that Alex Farber's email address is alexm @ somewhere... a lot of coincidences for a name I would not consider extremely common.  

...or maybe I just read too much into things...  darn conspiracies are everywhere!




0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9873158
This is not conspiracy. AlexF identifier is usually busy in any place (this name is very popular).
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9874253

your kidding right?  What is the origin of "AlexF"?

I mean... AxelF I can see... (it's a song, a band name (relatively local here), the character name of a cop in Beverly Hills Cop, etc)

...eh... In either case, good link!

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question