在Windows Phone中同步ReverseGeocodeQuery处理程序?

时间:2013-06-10 19:18:39

标签: c# multithreading windows-phone reverse-geocoding

我有疑问。我有一个ReverseGeocodeQuery,它会向我提供有关我的GPS位置的详细信息。现在我想同步几个ReverseGeocodeQuerys(每个都用于其他GPS位置)。但是,当我在一瞬间启动所有Query时,它们将不会执行...

有同步查询的简单方法吗?

for(int i = 0; i<mySimulation.Count(); i++)
                {

                    if (i==0)
                    {
                        AddMapLayer(mySimulation.ElementAt(i).Coordinate, Colors.Yellow, false);

                        myReverseGeocodeQuery_1 = new ReverseGeocodeQuery();
                        myReverseGeocodeQuery_1.GeoCoordinate = mySimulation.ElementAt(i).Coordinate;
                        myReverseGeocodeQuery_1.QueryCompleted += ReverseGeocodeQuery_QueryCompleted_1;
                        myReverseGeocodeQuery_1.QueryAsync();
                    }

                    if (i == 1)
                    {
                        AddMapLayer(mySimulation.ElementAt(i).Coordinate, Colors.Orange, false);

                        myReverseGeocodeQuery_2 = new ReverseGeocodeQuery();
                        myReverseGeocodeQuery_2.GeoCoordinate = mySimulation.ElementAt(i).Coordinate;
                        myReverseGeocodeQuery_2.QueryCompleted += ReverseGeocodeQuery_QueryCompleted_2;
                        myReverseGeocodeQuery_2.QueryAsync();

1 个答案:

答案 0 :(得分:0)

您可以使用 AutoResetEvent

AutoResetEvent done = new AutoResetEvent(true);
...
for(int i = 0; i<mySimulation.Count(); i++)
{
    // Is somebody working?
    // If so I wait...
    done.WaitOne();

    if (i==0)
    {
        ....
        myReverseGeocodeQuery_1.QueryCompleted += ReverseGeocodeQuery_QueryCompleted_1;
        done.Reset(); // Hey I'm working, wait!
        myReverseGeocodeQuery_1.QueryAsync();
    }
    ...
}
...
void ReverseGeocodeQuery_QueryCompleted_1(...)
{
    ...
    done.Set(); // I'm done! Your turn...
}

WaitOne 上阻止主线程 因为时间已晚)当任何地理查询正在运行时,避免同时启动另一个......