Search code examples
c#hololensazure-spatial-anchors

Application Suddenly Closed After Anchor Located


I'm recently developing Azure Spatial Anchor in Hololens. After following this tutorial from the Microsoft Website here, I tweak some of my code. It worked well so far. Until I try to find the anchor I have created. After my anchor is found (and the located anchor instantiated), my apps suddenly closed. It reach until the part where it shows text "Found!". Here is part of my code that finding the anchor.

    void Update(){
        lock (dispatchQueue)
        {
            if (dispatchQueue.Count > 0)
            {
                dispatchQueue.Dequeue()();
            }
        }

    }

    protected void QueueOnUpdate(Action updateAction)
    {
        lock (dispatchQueue)
        {
            dispatchQueue.Enqueue(updateAction);
        }
    }


    private void CloudSpatialAnchorSession_AnchorLocated(object sender, AnchorLocatedEventArgs args){
        switch (args.Status){
            case LocateAnchorStatus.Located:
                QueueOnUpdate(() =>{
                    var cube = GameObject.Instantiate(cubePrefab) as GameObject;
                    cube.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    cube.AddComponent<WorldAnchor>();
                    cube.GetComponent<UnityEngine.XR.WSA.WorldAnchor>().SetNativeSpatialAnchorPtr(args.Anchor.LocalAnchor);
                    cubeMaterial = cube.GetComponent<Renderer>().material;
                    cubeMaterial.color = Color.red;

                    this.cubes.Add(cube);
                    cloudAnchorId = "";
                    this.msg.text = "Found!";
                    Task.Run(async () =>{
                        await Task.Delay(1000);
                    });
                });
                break;
            case LocateAnchorStatus.AlreadyTracked:
                this.msg.text = "ASA Info: Anchor already tracked. Identifier: " + args.Identifier;
                break;
            case LocateAnchorStatus.NotLocated:
                this.msg.text = "ASA Info: Anchor not located. Identifier: " + args.Identifier;
                break;
            case LocateAnchorStatus.NotLocatedAnchorDoesNotExist:
                this.msg.text = "ASA Error: Anchor not located does not exist. Identifier: " + args.Identifier;
                break;
        }
    }
    private void CloudSpatialAnchorSession_LocateAnchorsCompleted(object sender, LocateAnchorsCompletedEventArgs args){
        this.msg.text = "ASA Info: Locate anchors completed. Watcher identifier: " + args.Watcher.Identifier;
        Task.Run(async () =>{
            await Task.Delay(2500);
        });
        args.Watcher.Stop();
    }

I used Azure Spatial Anchor SDK v1.1.0, Unity3D 2019.1.10f, and Visual Studio 2017.

anyone know the reason?


Solution

  • Since most Unity Engine API can only be called in the main thread, updates to the UI updates can only happen on the main thread.

    More information please see:Thread safety in Unity