Search code examples
c#windows-runtimetask-parallel-librarywinrt-async

Parallel loop in winrt


for (int j = 0; j < 10; j++)
{
    for (long i = 0; i < bound / 10; i++)
    {
        routeLine.Locations.Add(new Location
        {
            Latitude = ((BingMapsRESTService.Route)(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[k][0],
            Longitude = ((BingMapsRESTService.Route)(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[k][1]
        });

        k++;
    }

    await Task.Delay(TimeSpan.FromMilliseconds(1));

    Temp("Drawing Route (" + ((j * 10)/2).ToString() + "%)"); // to show progress,Temp sets text property of a textbox
}

Bound has value between 6000 to 10000. This loop takes time and hang the UI that's why i divided the loop into 10 parts and used task.delay. Is it possible to run all ten loops in parallel? I can't use thread as I can't create object of Location class in new thread. It throws an error of task being marshaled by some another thread


Solution

  • Yes it is possible; you can use TPL and Parallel.ForEach:

    TPL

    Parallel.ForEach