并行对象初始化

时间:2013-06-14 11:51:07

标签: c# windows-runtime initialization task-parallel-library

我需要解释一下如何使用C#以并行方式创建大量对象。现在我正在做一个非常懒惰的事情(见底部的例子)。我想使用并行性来提高性能,因为我的应用程序需要超过10秒才能初始化所有这些对象。

        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)
         }

我这样做了88次而没有使用for循环,因为我需要填充每一个LocationCollection。到目前为止,我还没有找到任何其他解决方案。 提前谢谢

1 个答案:

答案 0 :(得分:1)

最后,我们没有在非常严格的术语中使用并行化。我们只使用异步方法,其中所有初始化对象都由一个单独的线程构成。通过这种方式,我们将作业分为两部分:一部分用于初始化,另一部分用于其余的应用程序。

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....
}

然后我们调用这样的方法:

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

     // rest of initialization process
}

使用“ConfigureAwait(false)”,我们可以让线程将其与主要线程分开。我们不需要将它与主要的同步,因此我们将“false”作为参数放在Method

相关问题