This is a migrated thread and some comments may be shown as answers.

RadChart multithread safe

6 Answers 109 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Tebba
Top achievements
Rank 1
Tebba asked on 21 Feb 2012, 12:17 PM
Hello,

How can i make RadChart multi-thread safe ?
I get the following error "Non unique name for palette in collection." when i try to run it in multi-thread environment the following statemenet  "RadChart chart = new RadChart();"

Thanks,

6 Answers, 1 is accepted

Sort by
0
Tebba
Top achievements
Rank 1
answered on 23 Feb 2012, 02:40 PM
anyone ? 
0
Petar Marchev
Telerik team
answered on 24 Feb 2012, 01:21 PM
Hello Tebba,

Could you elaborate a bit more on how did you run the chart in multi-thread environment as our local tests were unable to reproduce the described exception. Below I have pasted the code I have used in a new project and it does not throw any exception.
protected void Page_Load(object sender, EventArgs e)
{
 Thread t = new Thread(new ThreadStart(() =>
 {
   Thread.Sleep(3000);
   RadChart rc = new RadChart();
 }));
 
 t.Start();
}

Have you tried running your code in the main thread?


Greetings,
Petar Marchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Tebba
Top achievements
Rank 1
answered on 24 Feb 2012, 02:52 PM
Hello,

Thanks for responding. What it think it happens is:

In the RadChart constructor is the following line
this.chart = new Chart(this);

So this creates an instance of Chart. The Chart class has a property named paletteCollection which is:
internal ChartSeriesPaletteCollection paletteCollection = new ChartSeriesPaletteCollection(); This property is going to be initialized when the constructor of Chart is called. This init is going to be executed for every instance of Chart and RadChart created.

The ChartSeriesPaletteCollection class has a property named List which is static. The constructor of ChartSeriesPaletteCollection initialize the list 
ChartSeriesPaletteCollection.List = new ArrayList();
and then fills up the list:
this.Add(new ChartSeriesPalette("Default"new Color[]
    {
        Color.FromArgb(150150150),
        Color.FromArgb(215215214),
        Color.FromArgb(230230230),
        Color.FromArgb(241241241),
        Color.FromArgb(224224224)
    }, new Color[]
    {
        Color.FromArgb(194194194),
        Color.FromArgb(241241241),
        Color.FromArgb(249249249),
        Color.FromArgb(248249248),
        Color.FromArgb(255255255)
    }));

The add method check if the palette exists and if exists throws an error.

if (this.Get(value.Name) != null)
    {
        throw new Exception("Non unique name for palette in collection.");
    }

So the List property, which is static, is going to be renewed for every new created instance of chart.

Now if 2 threads come in the same time, and both threads execute the statement List = new ArrayList(); and then the add statement, one of them is going to fail :).

I think now is pretty obvious where the problem is, but please tell me if you need more details.

PS: The List property should be in a static constructor. Doing this, it will only be initialized once and is guaranteed to be thread safe.



0
Tebba
Top achievements
Rank 1
answered on 24 Feb 2012, 02:55 PM
What you did in the page load is not going to reproduce my problem, because you are not creating multiple threads, you are creating a thread that creates a RadChart.

Thank you,
0
Accepted
Giuseppe
Telerik team
answered on 29 Feb 2012, 12:14 PM
Hello Tebba,

It seems you are referring to the now obsolete RadChart for ASP.NET and not to the RadChart for ASP.NET AJAX control indicated in this ticket as the latter does not have either ChartSeriesPalette, or ChartSeriesPaletteCollection classes. As a matter of fact the whole RadControls for ASP.NET suite has been discontinued for some time now in favor of the RadControls for ASP.NET AJAX suite and we neither distribute RadControls for ASP.NET, nor do we support it or fix any issues with it any more.

We would suggest you to upgrade to RadControls for ASP.NET AJAX (and RadChart for ASP.NET AJAX) that seems to behave as expected in a scenario where multiple threads are created like this:
protected void Page_Load(object sender, EventArgs e)
{
    Thread t = new Thread(new ThreadStart(() =>
    {
        while (true)
        {
            RadChart rc = new RadChart();
        }
    }));
 
    Thread t2 = new Thread(new ThreadStart(() =>
    {
        while (true)
        {
            RadChart rc = new RadChart();
        }
    }));
 
    Thread t3 = new Thread(new ThreadStart(() =>
    {
        while (true)
        {
            RadChart rc = new RadChart();
        }
    }));
 
    t.Start();
    t2.Start();
    t3.Start();
}



Greetings,
Giuseppe
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Tebba
Top achievements
Rank 1
answered on 29 Feb 2012, 03:06 PM
Hi,

Problem solved.
 
Thank you very much.

Tags
Chart (Obsolete)
Asked by
Tebba
Top achievements
Rank 1
Answers by
Tebba
Top achievements
Rank 1
Petar Marchev
Telerik team
Giuseppe
Telerik team
Share this question
or