Page 1 of 1

Handle increase - TChart in new thread

Posted: Thu Mar 18, 2010 2:21 pm
by 15654836
Hi,
I want to create charts which shall be stored in a database. So the charts are not shown in a UI or something.
The chart is periodically created after a specific time span. Responsible for the chart creation is a new thread which runs in the background.
The problem I noticed is that the handle count increases even though I call Dispose() on the chart object.

Code: Select all

public void test()
		{
			for ( int i = 0; i < Int32.MaxValue; i++ )
			{
				Thread thread = new Thread( new ThreadStart( delegate()
				{
					// Lock the image creation because TeeChart seems to use GDI functions
					// (in the ContourMapControls constructor) which are not thread-safe.
					test();
				} ) );

				thread.Name = "Handle increase demo";
				thread.Priority = ThreadPriority.BelowNormal;
				thread.Start();
			}
		}

		private void test1()
		{
			TChart tchart = new TChart();

			try
			{
			}
			finally
			{
				tchart.Dispose();
			}

			Thread.Sleep( 10 );
		}
The application runs on a server system which means we cannot just shut down and start the application again.
Do I use something wrong or is there a alternative?

I also attached a sample project.

Thanks.

SurveyBob

Re: Handle increase - TChart in new thread

Posted: Thu Mar 18, 2010 2:28 pm
by 15654836
Sorry, take this sample project.

Re: Handle increase - TChart in new thread

Posted: Fri Mar 19, 2010 4:04 pm
by narcis
Hi SurveyBob,

As you have already noticed, TeeChart is not thread-safe. Have you tried doing something similar without using threads? Do you get the same behavior?

Also, how do you know handle count is increasing?

Please notice that Dispose doesn't free objects immediately, it's garbage collector who does that task in the .NE Framework. Therefore, deterministic finalization is not available in .NET Framework as it's explained here:

http://www.geekinterview.com/question_details/5532
http://stackoverflow.com/questions/1884 ... ation-in-c

Re: Handle increase - TChart in new thread

Posted: Fri Mar 19, 2010 4:32 pm
by 15654836
Hi Narcis,

yes, I did the same without using a separate thread and the handle count did not increase. However, the handle count also increases using a simple .NET Control object instead of a TChart object. So this is a general problem because a Control object could be everything such as a button or a drop-down box.

I am aware of the garbage collections behavior but I ran several tests with a duration of at least 14 hours and the handle count did not decrease a single time during the test.
You can see the handle count when you open the Task Manager ("Process" tab). Under the "View" menu entry, you can choose "Select Columns..." to insert the handle column. Afterwards you start the application which you'll then find in the list under the "Process" tab.

I have an idea why the handle count increases but nothing specific yet and since this is not just a TChart issue I keep on investigating on my own. Maybe I find a solution for that.

Thanks for your help.

SurveyBob

Re: Handle increase - TChart in new thread

Posted: Fri Mar 19, 2010 4:42 pm
by narcis
Hi SurveyBob,

Thanks for your feedback.
I have an idea why the handle count increases but nothing specific yet and since this is not just a TChart issue I keep on investigating on my own. Maybe I find a solution for that.
Probably the handle count increases during application's execution but it may get a point when it becomes stable as new objects are created and destroyed at a similar pace.