1

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

spaghettifunk
  • 1,936
  • 4
  • 24
  • 46
  • Is the `LocationCollection` thread-safe? – Jobo Jun 14 '13 at 11:53
  • 3
    Even doing that 88 times should only take milliseconds. Something else must surely be taking 10 seconds. – Matthew Watson Jun 14 '13 at 11:54
  • @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.locationcollection.aspx](http://msdn.microsoft.com/en-us/library/microsoft.maps.mapcontrol.locationcollection.aspx) – spaghettifunk Jun 14 '13 at 11:56
  • Oh, i didn´t know it´s a framework class. – Jobo Jun 14 '13 at 12:00
  • What does your profiling tell you? Why does it take so long? – svick Jun 14 '13 at 12:29

1 Answers1

1

At the end, we hadn't use the parallelization in the very strict of the term. We simply use async method where all the initialization objects are made by a separate thread. In this way we split the job in two parts: one for the initialization and one for the rest of application.

private async static Task<bool> CreateLocations()
{
    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),
            ......
    }

    // and so on....
}

And then we call the method like this:

public void SetupMap()
{
     Factory.CreateLocations().ConfigureAwait(false);

     // rest of initialization process
}

With "ConfigureAwait(false)" we can let a thread to separate itself from the main one. We did not need to synchronize it with the main one then, so we put "false" as parameter inside the Method

spaghettifunk
  • 1,936
  • 4
  • 24
  • 46