Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need some explanation in how to create a lot of objects in a parallel way using C#. Right now I'm doing a very lazy thing (see the example at the bottom). I'd like to improve the performance using parallelism because my application takes longer than 10 seconds to initialize all those objects.

        LocationCollection collection = new LocationCollection() 
        {
            new Location( 45.516020899111012,9.121949242919207),
            new Location( 45.515890001741056,9.12163291732332),
            new Location( 45.515769306159115,9.121201707799385),
            new Location( 45.515713976667044,9.120921331149775),
            new Location( 45.516101870996565,9.120109674115509),
            new Location( 45.517649612704567,9.116948581756963),
            new Location( 45.518057566952308,9.116076542009536),
            new Location( 45.518131625236613,9.115917929540883),
            new Location( 45.518670136997606,9.114769836460944),
            new Location( 45.519004561368767,9.114144538020609),
            new Location( 45.522601162665104,9.107672668774397),
            new Location( 45.522748862809266,9.109105402458235),
            new Location( 45.523972603875457,9.10865818071991),
            new Location( 45.524045083673286,9.108966406046985),
            new Location( 45.523423302236786,9.109341605674809),
            new Location( 45.523092661828628,9.109803152708732),
            new Location( 45.522818514726829,9.110530052388302),
            new Location( 45.522246352996028,9.111013842048367),
            new Location( 45.521746927840852,9.111578624890933),
            new Location( 45.520781496237099,9.112948113338327),
            new Location( 45.52043700147,9.114788655024009),
            new Location( 45.520293766461208,9.11598042287495),
            new Location( 45.520028393083059,9.116803240629514),
            new Location( 45.519747394472901,9.11727749496557),
            new Location( 45.518959913236941,9.118230512071632),
            new Location( 45.51901582000967,9.118394197027454),
            new Location( 45.519046672303304,9.118457960354206),
            new Location( 45.519912005862544,9.117775334469274),
            new Location( 45.519973990870028,9.117937113800979),
            new Location( 45.52162009603299,9.117660191651888)
         }

I'm doing this 88 times without using a for loop because I need to fill every single LocationCollection. So far, I haven't found any other solution. Thank you in advance

share|improve this question
Is the LocationCollection thread-safe? – Jobo 49 mins ago
3  
Even doing that 88 times should only take milliseconds. Something else must surely be taking 10 seconds. – Matthew Watson 48 mins ago
@Jobo: According with Microsoft MSDN Library, "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe" link http://msdn.microsoft.com/en-us/library/microsoft.maps.mapcontrol.locationcollec‌​tion.aspx – Dado 47 mins ago
   
Oh, i didn´t know it´s a framework class. – Jobo 43 mins ago
What does your profiling tell you? Why does it take so long? – svick 13 mins ago

1 Answer

Assumption: I suppose it takes a long time because inside Location's constructor some expansive computation is made (otherwise what you have to make parallel is not initialization but the place where time is consumed).

Step 1: to make it parallel (and little bit more clean, I suggest you move all that data constants into a configuration file or an embedded resource).

IEnumerable<Location> GetLocations()
{
    yield return new Location(45.52162009603299,9.117660191651888);
    // ... and so on but see previous comment...
}

Then:

Parallel.ForEach(GetLocations(), x =>
{
    lock (locations) locations.Add(x);
});

I stress again about the first sentence in this answer: this assumes that the expansive computation is inside the constructor of Location (or inside the Add method of LocationCollection, I don't have that DLL to decompile to see what's inside) and not somewhere else in the code. If, for example, the time is consumed to build the map or to contact the service through Internet then to make parallel this small task won't increase performance at all (it may even make it worse because of threading overhead).

share|improve this answer
This won't execute the Location constructors in parallel and it won't Add() in parallel either. So there is absolutely no gain from parallelizing it this way. – svick 14 mins ago
@svick: What do you suggest then? – Dado 8 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.