Solved

Use of threads

Posted on 2003-12-03
4
386 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now