1

我需要一些关于如何使用 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)
         }

我在不使用 for 循环的情况下这样做了 88 次,因为我需要填充每个 LocationCollection。到目前为止,我还没有找到任何其他解决方案。先感谢您

4

1 回答 1

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里面

于 2013-06-17T09:12:54.973 回答