Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
System.Threading.Tasks.Parallel.ForEach(ktl,rc=>
                           {
                               ++i;

                               g.DrawRectangle(pen, rc);

                               g.DrawString("KN" + i.ToString(), new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold),
                      new SolidBrush(Color.Blue), rc.X, rc.Y);
                           }
                       );


but this works

C#
foreach (Rectangle rc in ktl)
                    {

                        ++i;
                        g.DrawRectangle(pen, rc);

                        g.DrawString("KN"+i.ToString(), new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold),
               new SolidBrush(Color.Blue), rc.X, rc.Y);

                    }
Posted

1 solution

At the bottom of the MSDN-page for the Graphics[^]-class they state:

"Any public static members of this type are thread safe. Any instance members are not guaranteed to be thread safe."

Sidenote: Any object of a class that implements IDisposable should be disposed after use, which can be easiest achieved by using it within a using-block. You should do this with your Font and Solidbrush. As it's always the same Font and SolidBrush, there's no need to allocate a new one in each iteration of your loop anyway.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900