0

它来自 Windows Phone 项目。我正在尝试调用几个处理程序,一个处理程序一个处理程序来接收有关 GPS / 反向位置的信息。我想知道为什么它不能正确运行。

当我只设置 1 个坐标时就可以了。我有关于 Street 等的消息。但是当有更多坐标时,我的处理程序不会被调用。

private async void SimulationResults()
        {
            done = new AutoResetEvent(true);
            Geolocator geolocator = new Geolocator();
            geolocator.DesiredAccuracy = PositionAccuracy.High;

            myCoordinate = new GeoCoordinate(51.751985, 19.426515);


            if (myMap.Layers.Count() > 0) myMap.Layers.Clear();

            mySimulation = new List<SimulationItem>();
            mySimulation = Simulation.SimulationProcess(myCoordinate, 120); // Odległość

            for(int i = 0; i<2; i++)
            {
                done.WaitOne();
                if (mySimulation.ElementAt(i).Id == 1 | mySimulation.ElementAt(i).Id == -1)
                {
                    // Oczekiwanie, ponieważ obiekt jest zasygnalizowany od razu wejdziemy
                    // do sekcji krytycznej
                    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;
                    // Sekcja krytyczna
                    done.Reset(); // Hey I'm working, wait!
                    myReverseGeocodeQuery_1.QueryAsync();

                }
            }
            MessageBox.Show("Skonczylem");
        }

        private void ReverseGeocodeQuery_QueryCompleted_1(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
        {
            done.Set();
            if (e.Error == null)
            {
                if (e.Result.Count > 0)
                {
                    MapAddress address = e.Result[0].Information.Address;
                    MessageBox.Show("Wykonano "+address.Street);

                }
            }
        } 
4

1 回答 1

1

这里发生的事情是您AutoResetEvent在 UI 线程上阻塞,但这与ReverseGeocodeQuery尝试运行的线程相同。由于它被阻止,它无法运行,也无法调用您的回调。

下面是一个非常快速的解决方案,它不会过多地改变您的流程并假设某种“在所有事情上都完成做 X”的要求。我在后台线程上触发了整个事情:

    new Thread(new ThreadStart(() =>
        {
            SimulationResults();
        })).Start();

由于以下所有内容都在后台线程上,我需要Dispatcher.BeginInvoke()围绕调用 UI 线程的任何内容添加一些调用,但这样被阻塞的线程是您的后台线程,而不是您的 UI 线程。

    AutoResetEvent done;
    int remaining;

    private async void SimulationResults()
    {
        done = new AutoResetEvent(true);
        Geolocator geolocator = new Geolocator();
        geolocator.DesiredAccuracy = PositionAccuracy.High;

        var myCoordinate = new GeoCoordinate(51.751985, 19.426515);


        var mySimulation = new List<GeoCoordinate>()
        {
            new GeoCoordinate(51.751985, 19.426515),
            new GeoCoordinate(2, 2)
        };
        //mySimulation = Simulation.SimulationProcess(myCoordinate, 120); // Odległość

        remaining = mySimulation.Count;

        for (int i = 0; i < mySimulation.Count; i++)
        {
            done.WaitOne();
            //if (mySimulation.ElementAt(i).Id == 1 | mySimulation.ElementAt(i).Id == -1)
            //{
                // Oczekiwanie, ponieważ obiekt jest zasygnalizowany od razu wejdziemy
                // do sekcji krytycznej
                //AddMapLayer(mySimulation.ElementAt(i).Coordinate, Colors.Yellow, false);

            var tempI = i;

            Dispatcher.BeginInvoke(() =>
            {

                var myReverseGeocodeQuery_1 = new ReverseGeocodeQuery();
                myReverseGeocodeQuery_1.GeoCoordinate = mySimulation.ElementAt(tempI);
                myReverseGeocodeQuery_1.QueryCompleted += ReverseGeocodeQuery_QueryCompleted_1;
                // Sekcja krytyczna
                done.Reset(); // Hey I'm working, wait!
                myReverseGeocodeQuery_1.QueryAsync();
            });

            //}
        }

    }

    private void ReverseGeocodeQuery_QueryCompleted_1(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
    {
        done.Set();

        remaining--;

        if (e.Error == null)
        {
            if (e.Result.Count > 0)
            {
                MapAddress address = e.Result[0].Information.Address;
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Wykonano " + address.Street);
                });

            }
        }

        if (remaining == 0)
        {
            // Do all done code
            Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show("Skonczylem");
            });
        }
    } 

或者,您也可以将其设为适当的异步方法,等待不同事件的进展。

于 2013-06-17T06:32:03.460 回答