I am working with some DLLs that I cannot change in this case it is the Microsoft Band SDK. I have wrapped all the uwp specific code in compiler directives for NetFX_CORE and I have all the appropriate references/capabilities in both the Assembly_csharp project and the windows holographic project. I have tried 3 different methods all achieving the same results. 1st method: async start with await keywords 2nd method: FunctionAsync().GetAwaiter().GetResult(); 3rd method: Start => initialize Tasks, Update check if task is complete and if it is then process result All three succeed on short running processes and fail on long running processes. Example: Code (CSharp): _bands = await BandClientManager.Instance.GetBandsAsync(); //Retrieves list of devices _bands = BandClientManager.Instance.GetBandsAsync().GetAwaiter().GetResult(); //Passes _client = await BandClientManager.Instance.ConnectAsync(_bands[0]); //Fails with "A Method was called at an unexpected time" _client = BandClientManager.Instance.ConnectAsync(_bands[0]).GetAwaiter().GetResult(); //Fails with "A Method was called at an unexpected time" even method 3 which I figured would let the task keep running and just catch it when it is complete also faults. I am looked and cant seem to find anyone else having this problem. I saw an app in the WS that gets around it but I cant seem to figure out how. Anyone have any suggestions?
All 3 methods should work. It is hard to tell without looking at your actual code. Can you share a mininum repro?
Hello, Can you share the script or code on how you are using this? Would be Helpful to have more information to debug the issue. Thank you, Wesley
Here is a Vanilla attempt directly from the Microsoft Band SDK Docs. BTW Thanks I have spent several weeks off and on trying to solve this problem. I stripped out the try catch for simplicity. Code (CSharp): using UnityEngine; #if NETFX_CORE using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; //Imported from the Microsoft BAND SDK using Microsoft.Band; using System.Diagnostics; #endif public class BandManager : MonoBehaviour { #if NETFX_CORE private IBandInfo[] _bands; private IBandClient _client; #endif #if NETFX_CORE // Use this for initialization async void Start () { _bands = await BandClientManager.Instance.GetBandsAsync(); var band = _bands.First(); //Code Breaks after waiting at this line, Same code works in uwp XAML app. _client = await BandClientManager.Instance.ConnectAsync(band); } #endif // Update is called once per frame void Update () { } }
This doesn't really seem to be related to Unity: it's just using the API incorrectly somehow. Maybe you're calling it on the wrong thread? I'm not very familiar with that SDK so I'm not sure why it fails.
have you tried to check if band has a correct value? the await on 27 returns control to the main thread which can do some things that break the execution of line 31 if would generally use _bands.FirstOrDefault() on 29 to make sure it won't crash if bands is empty...and then check for null before you invoke the connect....additionally if would also try to lock the _bands not to generate a race-condition occuring (depending on what you do with Start....) and have you tried this already? var bands = await BandClientManager.Instance.GetBandsAsync(); if (!bands.Any()) { //apply some logic here when there is no band detected.... return; } _band = await BandClientManager.Instance.ConnectAsync(bands[0]);