【问题标题】:"invalid cross-thread access" when calculating a route - Windows phone 8计算路由时出现“无效的跨线程访问” - Windows phone 8
【发布时间】:2013-09-06 10:55:41
【问题描述】:

我正在尝试在地图上显示路线,但出现此异常。我不知道为什么。 这是我的代码隐藏:

public partial class map_new : PhoneApplicationPage
{
    public GeoCoordinate destination;
    public GeoCoordinate myPosition;
    public Geolocator myGeolocator;
    List<GeoCoordinate> waypoints = new List<GeoCoordinate>();
    public RouteQuery routeQuery;


    public map_new()
    {
        InitializeComponent();
        destination = new GeoCoordinate(41.909859, 12.461792);
        ShowDestinationLocationOnTheMap();

        myGeolocator = new Geolocator();
        myGeolocator.DesiredAccuracy = PositionAccuracy.High;
        myGeolocator.MovementThreshold = 20; // The units are meters.
        myGeolocator.StatusChanged+=geolocator_StatusChanged;


    }

    private async Task update_position() 
    {
        Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
        Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
        myPosition = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
        Dispatcher.BeginInvoke(() =>
        {
            this.myMap.Center = myPosition;
            this.myMap.ZoomLevel = 16;
        });
        //update_route();
    }

    private async void update_route()
    {
        await update_position();
        RouteQuery routeQuery = new RouteQuery();
        waypoints.Add(myPosition);
        waypoints.Add(destination);
        routeQuery.Waypoints = waypoints;
        routeQuery.QueryCompleted += routeQuery_QueryCompleted;
        routeQuery.QueryAsync();
    }

    void routeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
    {
        if (e.Error == null)
        {
            Route MyRoute = e.Result;
            MapRoute MyMapRoute = new MapRoute(MyRoute);
            myMap.AddRoute(MyMapRoute);
            routeQuery.Dispose();
        }
    }

    void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
    {
        string status = "";

        switch (args.Status)
        {

            case PositionStatus.Ready:
                // the location service is generating geopositions as specified by the tracking parameters
                status = "ready";
                update_route();
                break;

    }

我认为错误是 update_route() 不等待 update_position() 完成,但我不知道如何设置 update_route() 等待。 有什么想法吗?

编辑:应用 KooKiz 和 Stephen 解决方案后,错误位于此行:

RouteQuery routeQuery = new RouteQuery();

【问题讨论】:

    标签: c# multithreading map windows-phone


    【解决方案1】:

    “无效的跨线程访问”通常发生在尝试从后台线程更新 UI 时。在您的情况下,我相信这是您尝试更新 myMap 控件的时候。

    要从 UI 线程更新控件,可以使用 Dispatcher.BeginInvoke 方法:

    private async void update_position() 
    {
        Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
        Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
        myPosition = CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
        Dispatcher.BeginInvoke(() =>
        {
                this.myMap.Center = myPosition;
                this.myMap.ZoomLevel = 16;
        });
        //update_route();
    }
    

    编辑:显然,RouteQuery 对象也需要在 UI 线程上实例化。那我建议在 UI 上调用整个update_route 方法:

            case PositionStatus.Ready:
                // the location service is generating geopositions as specified by the tracking parameters
                status = "ready";
                Dispatcher.BeginInvoke(() => update_route());
                break;
    

    (然后从update_position 方法中删除Dispatcher.BeginInvoke

    【讨论】:

    • 应用了您的解决方案,但仍然出现错误,请检查更新后的主要问题
    • 好的,似乎可以工作,显示了一条路线,但紧接着'routeQuery.Dispose();'引发了NullReferenceException为什么?
    • @JacopoGrassi 将RouteQuery routeQuery = new RouteQuery(); 替换为routeQuery = new RouteQuery();,否则您将创建一个局部变量而不使用您之前声明的字段(public RouteQuery routeQuery; 部分)
    【解决方案2】:

    你应该避免async void。相反,使用async Task,它允许您使用await 返回的Taskasync void 只能用于事件处理程序。

    我的博客上有一个introduction to async,您可能会觉得有帮助。

    【讨论】:

    • 应用了您的解决方案,但仍然出现错误,请检查更新后的主要问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多