index > Visual C# General > Creating an Individual Thread for each User Control

Creating an Individual Thread for each User Control

In a separate posting, I explained how I was getting a strange graphics error with a tab page containing several User Controls:
http://forums.microsoft.com/MSDN/showpost.aspx?postid=790101&siteid=1

Each such page contains 3 to 25 such User Controls and each User Control is designed to display a Bar, Line, or Pie Chart. So there's definitely some heavy computations going on when a file is first opened.

It was suggested to me there that the main UI thread is getting bogged down and this is the cause of the problem. Thinking about this some, I decided to instantiate each User Control in its own thread. I don't have a lot of experience with threading but wrote this code to accomplish this:

// Create new thread for ChartPanel and execute it
ChartPanel chartPanel = new ChartPanel(model, qNum, question.Text);

// Monitor the special GraphInfo events that are fired by the ChartPanel controls
ChartPanel.GraphInfoEvent += new ChartPanel.GraphInfoHandler(ChartPanel_GraphInfoEvent);

// Prepare thread
Thread chartPanelThread = new Thread(new ThreadStart(chartPanel.Initiate));

// And start it
chartPanelThread.Start();


All of this code is inside a 'foreach' loop, so each ChartPanel (the user control) gets it's own thread.

My questions are these:
  1. Does this threading code look correct?
  2. What test could I perform inside of the ChartPanel to definitively confirm that it is running in its own thread?
  3. I know of the rule of not changing something like a property between threads (I know how to use InvokeRequired and BeginInvoke to get around this limitation) but there is no restriction about reading a property value, is there?
Sincerely,

Robert Werner
Vancouver, BC

rwerner

rwerner wrote:

It was suggested to me there that the main UI thread is getting bogged down and this is the cause of the problem. Thinking about this some, I decided to instantiate each User Control in its own thread.

Don't do that. You can try running the computations on a separate thread to make the UI more responsive, but the controls should run on the same UI thread as the Form they are hosted on.

rwerner wrote:

I know of the rule of not changing something like a property between threads (I know how to use InvokeRequired and BeginInvoke to get around this limitation) but there is no restriction about reading a property value, is there?

Yes there is.




Mattias, C# MVP
Mattias Sjögren
reply 2

You can use google to search for other answers

 

More Articles

• Automatically do something after X amount of days?
• Grid-like element for Windows Forms Project?
• Total newbie question regarding dll locations
• using IEnumerator to removing items
• Detecting Internet Connection???
• C# & Franson GPSTools SDK
• Appsetting!! am I doing it right?
• start process as system user.. c#
• MSDE and C#
• folder write Permmisions...
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• C# Webmail
• Persistant Presentation Layer...
• Setting Property
• The size and the position of Tool bar
• Interesting little bug
• .Net and Crystal Question
• reading data from disks in c#
• Getting process count?
• Replacing Nulls in database table
• Errors while creating mmc snap-in with V
• DateTime problem (julian date system)
• Make object that allows access thru [] (
• compact framework data types with symbia
• Administrating remote windows services
• Passive waiting

Hot Articles

• File system watcher not working!
• Serializing derived classes in XML
• How can I make datagrid display the capt
• server client data send problem
• How to resolve typelib references when u
• Using a recursive method with a Generic
• DataSet & DataTable
• VS2005 Image Library Gone!
• .NET 2.0 Windows Service
• data table
• Guru Help Required with COM DLL Integrat
• A simple question...
• DragDrop registratation did not succeed.
• Alpha value of a bitmap is not saving
• Copying files in CSharp

Recommend Articles

• Datagridview bound to two related tables?
• Window Service printing HTML
• How to search a Record in DataTable
• running an app in the background
• regex performance problem
• Why no multiple inheritance allowed in C#?
• Making a Send To Shortcut
• Scroll TabPages
• Disable close box in Title Bar ?
• Search a big generic list for an occuren
• HELP PLEASE - The type or namespace name
• How to Create CHM File Using C#?
• Form error message after upgrade form Be
• don't understand AppDomain.CurrentDomain
• abt system.threading.timer